alireza.salemian
alireza.salemian

Reputation: 576

Why EF Core not set Custom Annotation?

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?

Upvotes: 3

Views: 5285

Answers (1)

bricelam
bricelam

Reputation: 30375

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.

Upvotes: 3

Related Questions