c# - How to create dynamic type DbSet in Entity Framework .Net Core application?
问题描述
In application database from time to time new tables will be created. I need to access this tables without code changes and show tables content in web application. Probably, I could make reflection query to get new tables and their column names.
After that I can add dynamic DbSet to EntityFramework context:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity("EntityName", config =>
{
config.Property("Id").HasColumnType("int");
config.Property("Name").HasMaxLength(255);
config.HasKey("Id");
});
base.OnModelCreating(modelBuilder);
}
Unfortunately, when I run migration (dotnet ef migrations add MigrationName) Entity Framework shows migration error:
System.Reflection.AmbiguousMatchException: Ambiguous match found. at System.RuntimeType.GetMethodImplCommon(String name, Int32 genericParameterCount, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers) at System.RuntimeType.GetMethodImpl(String name, BindingFlags bindingAttr, Binder binder, CallingConventions callConv, Type[] types, ParameterModifier[] modifiers) at System.Type.GetMethod(String name, BindingFlags bindingAttr) at DynamicEF.AppContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\aleks\Documents\Visual Studio 2017\Projects\DynamicEF\DynamicEF\AppContext.cs:line 19 at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at System.Lazy
1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy
1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure
1 accessor) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0
1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Ambiguous match found.
I'm confused about "Ambiguous match found".
Where is my mistake? How I should change my code to fix migration process?
解决方案
推荐阅读
- python-3.x - AttributeError:“dict_values”对象没有属性“get”
- android - Google Lite 地图显示的信息比普通地图多得多
- javascript - 如何将多个 SVG 居中到同一点?
- c++ - 如何在 GDB 中以十六进制打印双精度位?
- c# - C# - 等待超时后如何取消任务?
- javascript - 获取“buffered.end(0)”的最后一个索引?
- java - 将 Java 枚举值链接到动态实例化的对象
- ruby-on-rails - Rails + Ahoy gem + 设置额外的 cookie
- r - 为什么类函数不将向量视为R中的向量?
- windows - 在我的 Powershell 脚本中,为什么管理员用户会看到 AD 用户对象的“启用”属性值,而另一个“管理员”用户却看不到?