Base solution for your next web application
Open Closed

Problem using inherit DbContext in EF Core #4938


User avatar
0
ivanosw1 created

Hi I have a custom base DbContext, to make it works i am using DefaultDbContextAttribute as suggested here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1119">https://github.com/aspnetboilerplate/as ... ssues/1119</a>. If I use DefaultDbContextAttribute for only one implemented context i got the error: Found more than one concrete type for given DbContext Type. If I use DefaultDbContextAttribute for all implemented context except of only one it works, but i think it's not the expected behavior.

This doesn't work

public abstract class BaseDbContext : AbpDbContext
{
        protected BaseDbContext (DbContextOptions options) : base(options)
        {
        }
}
[DefaultDbContextAttribute]
public class SecurityContext : BaseDbContext 
{
    public SecurityContext(DbContextOptions<SecurityContext> options) : base(options)
     {
     }
}
public class TestContext : BaseDbContext 
{
    public NuovoContext(DbContextOptions<TestContext > options) : base(options)
    {
    }
}
public class AdtContext : BaseDbContext 
{
    public AdtContext(DbContextOptions options) : base(options)
    {
    }
}

This works and use SecurityContext when BaseDbContext is needed

public abstract class BaseDbContext : AbpDbContext
{
        protected BaseDbContext (DbContextOptions options) : base(options)
        {
        }
}
public class SecurityContext : BaseDbContext 
{
    public SecurityContext(DbContextOptions<SecurityContext> options) : base(options)
     {
     }
}
[DefaultDbContextAttribute]
public class TestContext : BaseDbContext 
{
    public NuovoContext(DbContextOptions<TestContext > options) : base(options)
    {
    }
}
[DefaultDbContextAttribute]
public class AdtContext : BaseDbContext 
{
    public AdtContext(DbContextOptions options) : base(options)
    {
    }
}

Is It right to add DefaultDbContextAttribute to all new DbContext thats inherits from BaseDbContext ?


1 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Is It right to add DefaultDbContextAttribute to all new DbContext thats inherits from BaseDbContext ?

    No. Please follow that issue for updates.