Why EF Core not set Custom Annotation?

c# entity-framework-core

Question

In DbContext i declare model as below:

modelBuilder.Entity<FileStore>().Property(x => x.FileStoreId)
            .ValueGeneratedNever()
            .ForSqlServerHasDefaultValueSql("NewSequentialId()")
            .HasAnnotation("RowGuidColumn", true);

but after add migration, HasAnnotation("RowGuidColumn", true) not add any Annotation to Column:

 columns: table => new
            {
                FileStoreId = table.Column<Guid>(nullable: false, defaultValueSql: "NewSequentialId()"),
                CreationTime = table.Column<DateTimeOffset>(nullable: false),
            }

and i most add Annotation directly:

FileStoreId = table.Column<Guid>(nullable: false, defaultValueSql: "NewSequentialId()").**Annotation("RowGuidColumn", true)**

how i can add Annotation that Generate auto in Add-Migration?

1
1
2/25/2017 6:33:57 PM

Accepted Answer

The provider specifies which model annotations are copied/transformed into the migration operations. To do what you're asking, you'd need to override the provider-specific IMigrationsAnnotationProvider service.

optionsBuilder.UseSqlServer(connectionString)
  .ReplaceService<SqlServerMigrationsAnnotationProvider, MyMigrationsAnnotationProvider>();

And here's the implementation.

class MyMigrationsAnnotationProvider : SqlServerMigrationsAnnotationProvider
{
    public override IEnumerable<IAnnotation> For(IProperty property)
        => base.For(property)
            .Concat(property.GetAnnotations().Where(a => a.Name == "RowGuidColumn"));
}

From there you'd need to do something to turn it into SQL by overriding the provider-specific IMigrationsSqlGenerator service.

1
2/27/2017 4:52:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow