【发布时间】:2019-04-02 19:19:00
【问题描述】:
我需要在DbContext注册所有实体。
我创建了一个extention 用于使用Reflection 自动注册所有实体:
public static void RegisterAllEntity<BaseType>(this DbModelBuilder builder, params Assembly[] assmblies)
{
IEnumerable<Type> types = assmblies.SelectMany(x => x.GetExportedTypes())
.Where(x => x.IsClass && !x.IsAbstract && x.IsPublic && typeof(BaseType).IsAssignableFrom(x));
foreach (Type EntityType in types)
builder.Entity<EntityType>();
}
但它告诉我这个错误:
EntityType' 是一个变量,但用作类型
在这一行:
foreach (Type EntityType in types)
builder.Entity<EntityType>();
有什么问题?这个问题怎么解决???
【问题讨论】:
-
您将运行时
Type对象与编译时类型混合在一起,您需要Entity的重载,它接受类型object。 -
@thisextendsthat 请进一步指导我。我该怎么做?
-
你可以在这里找到东西stackoverflow.com/questions/44207451/…
-
@RedaTaha 我看到了,但它没有回答我
标签: c# reflection entity-framework-6