EntityType' 是一个变量,但在使用反射时像类型一样使用
EntityType' is a variable but is used like a type when use the Reflection
我需要在 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' is a variable but is used like a type
在这一行中:
foreach (Type EntityType in types)
builder.Entity<EntityType>();
有什么问题吗?我该如何解决这个问题???
通用参数需要在编译期间可解析。您需要使用反射在这样的循环中调用 Entity
方法。请检查 this 答案。
用法示例
...
MethodInfo method = typeof(DbModelBuilder).GetMethod("Entity");
MethodInfo generic = method.MakeGenericMethod(EntityType);
generic.Invoke(builder, null);
编辑:
还有 Chris , there is no need to use reflection because DbModelBuilder
provide RegisterEntityType 方法,它接受 Type 作为参数,例如:
builder.RegisterEntityType(EntityType);
EDIT2:克里斯
查看文档,我认为您想使用 DbModelBuilder.RegisterEntityType
而不是 DbModelBuilder.Entity
。前者的文档说:
This method is provided as a convenience to allow entity types to be registered dynamically without the need to use MakeGenericMethod in order to call the normal generic Entity method.
因此,您应该使用 builder.RegisterEntityType(EntityType);
而不是 builder.Entity<EntityType>();
值得一提的是,在这种情况下,通常会有一种非泛型方法采用 Type
对象,因此如果您以后发现自己在使用其他软件时遇到这种情况,请检查该非泛型方法- 带有 Type
参数的通用方法。
您将 EntityType 用作 foreach 循环中的变量,并且 EntityType 类似于 builder.Entity<EntityType>()
中的类型。例如,将变量名称从 EntityType 更改为 entityType,以便 C# 编译器可以理解您的代码
我需要在 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' is a variable but is used like a type
在这一行中:
foreach (Type EntityType in types)
builder.Entity<EntityType>();
有什么问题吗?我该如何解决这个问题???
通用参数需要在编译期间可解析。您需要使用反射在这样的循环中调用 Entity
方法。请检查 this 答案。
用法示例
...
MethodInfo method = typeof(DbModelBuilder).GetMethod("Entity");
MethodInfo generic = method.MakeGenericMethod(EntityType);
generic.Invoke(builder, null);
编辑:
还有 Chris DbModelBuilder
provide RegisterEntityType 方法,它接受 Type 作为参数,例如:
builder.RegisterEntityType(EntityType);
EDIT2:克里斯
查看文档,我认为您想使用 DbModelBuilder.RegisterEntityType
而不是 DbModelBuilder.Entity
。前者的文档说:
This method is provided as a convenience to allow entity types to be registered dynamically without the need to use MakeGenericMethod in order to call the normal generic Entity method.
因此,您应该使用 builder.RegisterEntityType(EntityType);
builder.Entity<EntityType>();
值得一提的是,在这种情况下,通常会有一种非泛型方法采用 Type
对象,因此如果您以后发现自己在使用其他软件时遇到这种情况,请检查该非泛型方法- 带有 Type
参数的通用方法。
您将 EntityType 用作 foreach 循环中的变量,并且 EntityType 类似于 builder.Entity<EntityType>()
中的类型。例如,将变量名称从 EntityType 更改为 entityType,以便 C# 编译器可以理解您的代码