I am building web APIs in ASP.NET Core 1.1.
I have a number different databases (for different systems) which have common base schemas for configuration items such as Configuration, Users and groups (about 25 tables in all). I am trying to avoid duplicating the quite extensive EF configuration for the shared part of the model by inheriting from a base class as shown in the diagram.
However, this does not work because of the Entity Framework (EF) requirement to pass DbContextOptions<DerivedRepository>
as a parameter to the constructor, where DerivedRepository
must match the type of the repository the constructor is called on. The parameter must then be passed down to the base DbContext
by calling :base(param)
.
So when (for example) InvestContext is initialised with DbContextOptions<InvestContext>
, it calls base(DbContextOptions<InvestContext>)
and EF throws an error because the call to the ConfigurationContext
constructor is receiving a parameter of type DbContextOptions<InvestContext>
instead of the required type DbContextOptions<ConfigurationContext>
. Since the options field on DbContext is defined as
private readonly DbContextOptions _options;
I can't see a way around this.
What is the best way to define the shared model once and use it multiple times? I guess I could create a helper function and call it from every derived context, but it's not nearly as clean or transparent as inheritance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…