Skip to content

Make it easier to flow custom annotations through to Migrations #10258

Description

@Rgl88

Custom annotations on properties are not included in generated Up/Down migration, but they are correctly included in the BuildTargetModel method
I want reorder columns in up method via my custom 'DisplayOrder' annotation.

Consider the following example:
Custom Annotations added to

[DbContext(typeof(TestDbContext))]
[Migration("13960818210148_TestDb_Migration_v1.0")]
partial class TestDb_Migration_v10
{
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
               modelBuilder.Entity("St.Domain.Financial.Bank", b =>
                {
				    ...
						
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd();

                    b.Property<string>("Description")
                        .HasMaxLength(512)
                        .HasAnnotation("ColumnOrder", 4);

                    b.Property<string>("Name")
                        .IsRequired()
                        .HasMaxLength(128)
                        .HasAnnotation("ColumnOrder", 2);
                    b.ToTable("Bank");
					
					...
                });
        }
}

but not exist in:

public partial class TestDb_Migration_v10 : Migration
{
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Bank",
                columns: table => new
                {
				    ...
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Description = table.Column<string>(type: "nvarchar(512)", maxLength: 512, nullable: true),
                    Name = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false)
					...
                });
        }
}

C:\Users\User1>dotnet --info
.NET Command Line Tools (2.0.0)

Product Information:
Version: 2.0.0
Commit SHA-1 hash: cdcd1928c9

Runtime Environment:
OS Name: Windows
OS Version: 10.0.14393
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.0.0\

Microsoft .NET Core Shared Framework Host
Version : 2.0.0
Build : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d

Activity

  1. ajcvickers commented on Nov 10, 2017

    @ajcvickers
    Contributor

    @Rgl88 Annotations are only propagated to Migrations if they have been configured to do so. The reason for this is so that Migrations doesn't generate diffs and scaffold a new migration when any annotation changes, since many annotations only affect runtime behavior, not the database schema.

    Currently this requires overriding and replacing the annotation provider for whichever database you are using. For example, for SQL Server:

    public class CustomAnnotationProvider : SqlServerMigrationsAnnotationProvider
    {
        public CustomAnnotationProvider(MigrationsAnnotationProviderDependencies dependencies) 
            : base(dependencies)
        {
        }
    
        public override IEnumerable<IAnnotation> For(IProperty property)
        {
            var baseAnnotations = base.For(property);
    
            var annotation = property.FindAnnotation("ColumnOrder");
    
            return annotation == null
                ? baseAnnotations
                : baseAnnotations.Concat(new[] { annotation });
        }
    }

    This must then be registered in OnConfiguring or AddDbContext:

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .ReplaceService<IMigrationsAnnotationProvider, CustomAnnotationProvider>()
                .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=TestDatabase;ConnectRetryCount=0");

    I'm re-purposing this bug to consider implementing an easier way of doing this.

  2. changed the title [-]Migration does not correctly include custom annotations[/-] [+]Make it easier to flow custom annotations through to Migrations[/+] on Nov 10, 2017
  3. reopened this on Nov 12, 2017
  4. added this to the Backlog milestone on Nov 17, 2017
  5. weitzhandler commented on Dec 26, 2018

    @weitzhandler
    Contributor

    @ajcvickers thanks for your helpful example!
    Would be nice if the interaction between OnModelCreating and the migration process can be soften up a bit.
    I opened a new issue.

  6. AndriySvyryd commented on Dec 13, 2024

    @AndriySvyryd
    Member

    Related: #17740

  7. added theissue type on Jun 15, 2025
  8. majbo commented on Aug 25, 2025

    @majbo
  9. roji commented on Aug 25, 2025

    @roji
  10. majbo commented on Aug 25, 2025

    @majbo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions