Open Closed

Entity Framework Core (Ef Core) to eXpress Persistent Object (XPO) #4040


User avatar
0
jim created

Hello everyone, I need to change the Data Layer on abp. Instead of Entity Framework Core (EfCore), I wanted to utilize DevExpress's eXpress Persistent Object (XPO), but it compelled me to use it. Do you have any resources that might assist me in completing this task? Alternatively, do you have any samples? I hope your kind and considering my concern thanks.

XPO Link: https://www.devexpress.com/products/net/orm/

  • ABP Framework version: v6.0.1
  • UI type: Angular / MVC / Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes
  • Exception message and stack trace: None
  • Steps to reproduce the issue:" None

3 Answer(s)
  • User Avatar
    0
    gterdem created
    Support Team Senior .NET Developer

    It is more accurate to create an article request about it at https://github.com/abpframework/abp/issues with community-article-request label that can reach to a wider audience.

    Someone from community may already have experience about it.

  • User Avatar
    0
    jim created

    Hi gterdem,

    I've submitted a community-article-requiest on Github, but I think it takes time to be noticed by the team? because until now there is no reply on this. But anyway, Is there any possible to add another Data Access for Abp? I want to utilized the Xpo from DevExpress which is required by my client. My problem here is that I'm having a hard times of manually configure and build a context of it. Is there any possible for this? can you please give me some insight on this. Thank you.

    I tried to copy the implementation of MongoDb Integration, but everything seems to fail. I plan to just add another Data Access layer and use DevExpress's Xpo ORM.

  • User Avatar
    0
    jeffbuot created

    Hi, you can try this:

    Install the DevExpress.Xpo.EFCore NuGet package in your project. Create a class that inherits from the DevExpress.Xpo.XpoDbContext base class, this class represents the database context.

    public class MyDbContext : XpoDbContext
    {
        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }
    }
    

    Create entity:

    [Persistent("MyEntity")]
    public class MyEntity : XPLiteObject
    {
        [Key]
        [Persistent("MyId")]
        public int MyId { get; set; }
    
        [Persistent("MyProperty")]
        public string MyProperty { get; set; }
    }
    

    If your model has relationship you can refer to this article: https://docs.devexpress.com/eXpressAppFramework/402958/business-model-design-orm/business-model-design-with-entity-framework-core/relationships-between-entities-in-code-and-ui

    Register the dbcontext and XpoDbContextOption in your module and define your custom repository:

    public interface IMyEntityRepository: IRepository<MyEntity >
    {
        Task<List<MyEntity>> GetListAsync(
            string filterText = null,
            string sorting = null,
            int maxResultCount = int.MaxValue,
            int skipCount = 0,
            CancellationToken cancellationToken = default);
    }
    
    public class MyEntityRepository: EfCoreRepository<MyDbContext, MyEntity >, IMyEntityRepository
    {
      public async Task<List<MyEntity>> GetListAsync(string filterText = null, string sorting = null,
            int maxResultCount = int.MaxValue, int skipCount = 0, CancellationToken cancellationToken = default)
        {
            var query = await GetQueryableAsync();
            query = query
                .WhereIf(filterText != null, d => d.Name.Contains(filterText))
                .OrderBy(string.IsNullOrWhiteSpace(sorting) ? MyEntityConsts.GetDefaultSorting(false) : sorting);
            return await query.PageBy(skipCount, maxResultCount).ToListAsync(cancellationToken);
        }
    }
    
    [DependsOn(typeof(AbpEfCoreModule))]
    public class MyModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddAbpDbContext<MyDbContext>(options =>
            {
                options.AddDefaultRepositories();
                options.AddRepository<IMyEntityRepository, MyEntityRepository>();
            });
        }
    }
    

    In your application service module you can:

    public class MyService : ApplicationService
    {
        private readonly IMyEntityRepository _myEntityRepository;
    
        public MyService(IMyEntityRepository myEntityRepository)
        {
            _myEntityRepository = myEntityRepository;
        }
    
        public async Task<MyEntity> GetAsync(int id)
        {
            return await _myEntityRepository.FirstOrDefaultAsync(x => x.MyId == id);
        }
    }
    
Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11