Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 287
alireza.salemian Avatar asked Jul 22 '26 02:07

alireza.salemian


1 Answers

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.

like image 115
bricelam Avatar answered Jul 23 '26 15:07

bricelam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!