我为EF Core添加了新的PropertyAttributeConvention:
field0,field1,"field,2",field3
继承自SqlServerConventionSetBuilder以添加此约定:
public class RowGuidColumnAttributeConvention : PropertyAttributeConvention<RowGuidColumnAttribute>{
public override InternalPropertyBuilder Apply(InternalPropertyBuilder propertyBuilder, RowGuidColumnAttribute attribute, MemberInfo clrMember)
{
propertyBuilder.HasAnnotation("RowGuidColumn", "TRUE", ConfigurationSource.DataAnnotation);
return propertyBuilder;
}}
然后在启动文件中替换它:
public class DefaultCoreConventionSetBuilder : SqlServerConventionSetBuilder
{
public DefaultCoreConventionSetBuilder(IRelationalTypeMapper typeMapper, ICurrentDbContext currentContext, IDbSetFinder setFinder)
: base(typeMapper, currentContext, setFinder)
{ }
public override ConventionSet AddConventions(ConventionSet conventionSet)
{
base.AddConventions(conventionSet);
var rowGuid = new RowGuidColumnAttributeConvention();
conventionSet.PropertyAddedConventions.Add(rowGuid);
conventionSet.PropertyFieldChangedConventions.Add(rowGuid);
return conventionSet;
}
}
但是在结果中没有任何约定添加到模型在迁移中创建。 任何人都可以帮助我吗?
答案 0 :(得分:2)
我解决了这个问题,
在启动文件中:
services.AddDbContext<DataContext>();
在dbcontext中覆盖OnConfiguring:
optionsBuilder
.ReplaceService<IConventionSetBuilder, DefaultCoreConventionSetBuilder>()
.ReplaceService<IMigrationsSqlGenerator, DefaultSqlServerMigrationSqlGenerator>()
.UseSqlServer(_configuration["ConnectionStrings:DbConnection"]);