Entity framework core 如何用EF Core中实现的自定义替换默认SqlServerConventionSetBuilder?
我为EF Core添加了新的PropertyAttributeConvention:Entity framework core 如何用EF Core中实现的自定义替换默认SqlServerConventionSetBuilder?,entity-framework-core,Entity Framework Core,我为EF Core添加了新的PropertyAttributeConvention: public class RowGuidColumnAttributeConvention : PropertyAttributeConvention<RowGuidColumnAttribute>{ public override InternalPropertyBuilder Apply(InternalPropertyBuilder propertyBuilder, RowGuidColumn
public class RowGuidColumnAttributeConvention : PropertyAttributeConvention<RowGuidColumnAttribute>{
public override InternalPropertyBuilder Apply(InternalPropertyBuilder propertyBuilder, RowGuidColumnAttribute attribute, MemberInfo clrMember)
{
propertyBuilder.HasAnnotation("RowGuidColumn", "TRUE", ConfigurationSource.DataAnnotation);
return propertyBuilder;
}}
然后在启动文件中替换它:
services.AddEntityFrameworkSqlServer()
.AddScoped<SqlServerConventionSetBuilder, DefaultCoreConventionSetBuilder>()
.AddScoped<SqlServerMigrationsSqlGenerator, DefaultSqlServerMigrationSqlGenerator>()
.AddDbContext<DataContext>();
services.AddEntityFrameworkSqlServer()
.addScope()文件
.addScope()文件
.AddDbContext();
但结果是,在迁移过程中并没有向模型创建添加任何约定。
有人能帮我吗?我解决了这个问题
在启动文件中:
services.AddDbContext<DataContext>();
services.AddDbContext();
并且在dbcontext中覆盖OnConfigurang:
optionsBuilder
.ReplaceService<IConventionSetBuilder, DefaultCoreConventionSetBuilder>()
.ReplaceService<IMigrationsSqlGenerator, DefaultSqlServerMigrationSqlGenerator>()
.UseSqlServer(_configuration["ConnectionStrings:DbConnection"]);
选项生成器
.ReplaceService()
.ReplaceService()
.UseSqlServer(_配置[“ConnectionString:DbConnection”]);
optionsBuilder
.ReplaceService<IConventionSetBuilder, DefaultCoreConventionSetBuilder>()
.ReplaceService<IMigrationsSqlGenerator, DefaultSqlServerMigrationSqlGenerator>()
.UseSqlServer(_configuration["ConnectionStrings:DbConnection"]);