How to create a Repository with EF core for a Value object in asp.net Boilerplate

aspnetboilerplate c# entity-framework-core

Question

I have created a second dbContext using the ABP .net core startup template. This all works fine, but the second dbContext is only used to extract data out of a view.

I have a class that maps to the view, but this view has no primary key. And if it were to have a primary key, it would be a composed key.

So I can't derive that class from Entity, so I chose to derive it from ValueObject.

But ABP doesn't create repositories for ValueObjects afaik.

So my question is, how to create a custom .net core repository for a ValueObject. (I only need an implementation of GetAll, since it will be a readonly repository)

1
0
6/26/2018 8:01:22 AM

Accepted Answer

You can derive from Entity and configure your view in OnModelCreating of your DbContext to map Id property to a column.

modelBuilder.Entity<YourEntity>().Property(t => t.Id).HasColumnName("YourColumn");

Otherwise you can get by dependency injection IDbContextProvider service and use it like a simple DbContext

private readonly IDbContextProvider<TDbContext> _dbContextProvider;

public YourService(IDbContextProvider<TDbContext> dbContextProvider)
{
    _dbContextProvider = dbContextProvider;
}

public void MethodName(){
    var context = _dbContextProvider.GetDbContext();
    context.YourEntity.OrderBy(a => a.Name);
}
1
6/26/2018 9:51:28 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow