在 DbContext 中我声明模型如下:
modelBuilder.Entity<FileStore>().Property(x => x.FileStoreId)
.ValueGeneratedNever()
.ForSqlServerHasDefaultValueSql("NewSequentialId()")
.HasAnnotation("RowGuidColumn", true);
但添加迁移后,HasAnnotation("RowGuidColumn", true) 不会向列添加任何注释:
columns: table => new
{
FileStoreId = table.Column<Guid>(nullable: false, defaultValueSql: "NewSequentialId()"),
CreationTime = table.Column<DateTimeOffset>(nullable: false),
}
我最直接添加注释:
FileStoreId = table.Column<Guid>(nullable: false, defaultValueSql: "NewSequentialId()").**Annotation("RowGuidColumn", true)**
我如何在添加迁移中添加生成自动的注释?
提供者指定将哪些模型注释复制/转换到迁移操作中。要执行您所要求的操作,您需要覆盖特定于提供者的IMigrationsAnnotationProvider
服务。
optionsBuilder.UseSqlServer(connectionString)
.ReplaceService<SqlServerMigrationsAnnotationProvider, MyMigrationsAnnotationProvider>();
这是实现。
class MyMigrationsAnnotationProvider : SqlServerMigrationsAnnotationProvider
{
public override IEnumerable<IAnnotation> For(IProperty property)
=> base.For(property)
.Concat(property.GetAnnotations().Where(a => a.Name == "RowGuidColumn"));
}
从那里你需要做一些事情,通过覆盖特定于提供者的方法将其转换为 SQLIMigrationsSqlGenerator
服务。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)