Entity framework core 如何首先为EF7代码中的所有其他配置定义BaseConfiguration?
我想为所有模型配置(使用fluent api)定义一个Entity framework core 如何首先为EF7代码中的所有其他配置定义BaseConfiguration?,entity-framework-core,Entity Framework Core,我想为所有模型配置(使用fluent api)定义一个基本配置(出于某种原因),因此我创建基本配置类: public class BaseConfigration<T> : EntityTypeBuilder<T> where T : class { public BaseConfigration(InternalEntityTypeBuilder builder) :base(builder) { } } 这给
基本配置
(出于某种原因),因此我创建基本配置
类:
public class BaseConfigration<T> : EntityTypeBuilder<T> where T : class
{
public BaseConfigration(InternalEntityTypeBuilder builder) :base(builder)
{
}
}
这给了我一个错误:
无法从“Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder”转换为“Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder”
如何操作?如异常消息所述,您使用的类型不正确。在模型创建过程中,您拥有
EntityTypeBuilder
对象<代码>InternalEntityTypeBuilder不应在用户代码中用作EF核心的内部,如文档中所述
要定义将应用于模型中每个实体类型并由EntityTypeConfiguration
派生的BaseConfiguration
,代码应按以下方式组织:
public class BaseConfiguration<T> where T : class
{
public BaseConfiguration(EntityTypeBuilder<T> entityTypeBuilder)
{
// Write fluent API code here which will be applied to all entityTypes in the model
entityTypeBuilder.HasKey("Id");
}
}
public class LoyaltyActivityConfig : BaseConfiguration<LoyaltyActivity>
{
public LoyaltyActivityConfig(EntityTypeBuilder<LoyaltyActivity> entityTypeBuilder)
: base(entityTypeBuilder)
{
// Write fluent API code here which will be applied to EntityType LoyaltyActivity only
entityTypeBuilder.Property(x => x.Title).HasMaxLength(100);
}
}
上述方法将调用BaseConfiguration
构造函数,然后调用entitypeconfiguration
构造函数,应用所有fluent API代码
这里没有什么要记住的
在BaseConfiguration
中,即使您有genericEntityTypeBuilder
也不能使用generic方法,因为T
是未知类型。您可以定义一个接口,该接口将由每个T(模型中的所有实体类型)实现,然后使用泛型方法。不过,使用非泛型方法也会得到相同的结果。这只是可读性的问题
您还可以在配置类中使用方法调用而不是构造函数来实现相同的功能
为了组织上述代码,EF6中的
IEntityTypeConfiguration
功能已在EF core中实现,并将在2.0版本中提供。更多信息如异常消息所述,您使用的类型不正确。在模型创建过程中,您拥有EntityTypeBuilder
对象<代码>InternalEntityTypeBuilder不应在用户代码中用作EF核心的内部,如文档中所述
要定义将应用于模型中每个实体类型并由EntityTypeConfiguration
派生的BaseConfiguration
,代码应按以下方式组织:
public class BaseConfiguration<T> where T : class
{
public BaseConfiguration(EntityTypeBuilder<T> entityTypeBuilder)
{
// Write fluent API code here which will be applied to all entityTypes in the model
entityTypeBuilder.HasKey("Id");
}
}
public class LoyaltyActivityConfig : BaseConfiguration<LoyaltyActivity>
{
public LoyaltyActivityConfig(EntityTypeBuilder<LoyaltyActivity> entityTypeBuilder)
: base(entityTypeBuilder)
{
// Write fluent API code here which will be applied to EntityType LoyaltyActivity only
entityTypeBuilder.Property(x => x.Title).HasMaxLength(100);
}
}
上述方法将调用BaseConfiguration
构造函数,然后调用entitypeconfiguration
构造函数,应用所有fluent API代码
这里没有什么要记住的
在BaseConfiguration
中,即使您有genericEntityTypeBuilder
也不能使用generic方法,因为T
是未知类型。您可以定义一个接口,该接口将由每个T(模型中的所有实体类型)实现,然后使用泛型方法。不过,使用非泛型方法也会得到相同的结果。这只是可读性的问题
您还可以在配置类中使用方法调用而不是构造函数来实现相同的功能
为了组织上述代码,EF6中的IEntityTypeConfiguration
功能已在EF core中实现,并将在2.0版本中提供。更多信息
public class BaseConfiguration<T> where T : class
{
public BaseConfiguration(EntityTypeBuilder<T> entityTypeBuilder)
{
// Write fluent API code here which will be applied to all entityTypes in the model
entityTypeBuilder.HasKey("Id");
}
}
public class LoyaltyActivityConfig : BaseConfiguration<LoyaltyActivity>
{
public LoyaltyActivityConfig(EntityTypeBuilder<LoyaltyActivity> entityTypeBuilder)
: base(entityTypeBuilder)
{
// Write fluent API code here which will be applied to EntityType LoyaltyActivity only
entityTypeBuilder.Property(x => x.Title).HasMaxLength(100);
}
}
new LoyaltyActivityConfig(modelBuilder.Entity<LoyaltyActivity>());