efcore icon indicating copy to clipboard operation
efcore copied to clipboard

ModelSnapshot is different after migration is added and then removed

Open esargent opened this issue 10 years ago • 10 comments

While looking at code that was about to get checked in on my project I noticed that the ApplicationDbContextModelSnapshot had changed, even though I had reverted a change I thought I wanted to make.

Specifically I added a migration with some changes to an existing entity, but didn't like them so I used dnx ef database update [prior_migration] and dnx ef migrations remove When I was checking out Team Explorer I noticed the differences were there.

Not sure it matters, because they seem functionally equivalent, but also unclear why they should be different when the model state is the same and wanted to bring it up in case something might cause issues in a more general release to broader audience.

The changes occur in all entities in the model (not just the one I tried to change) and looks something like this:

   modelBuilder.Entity("MyApp.Models.AnswerOption", b =>
            {
                b.ToTable("AnswerOption");

                b.Property<int>("Id")
                    .ValueGeneratedOnAdd();

                b.Property<int?>("QuestionId");

                b.Property<string>("Text");

                b.HasKey("Id");

                b.HasIndex("QuestionId");
            });

to

        modelBuilder.Entity("MyApp.Models.AnswerOption", b =>
            {
                b.Property<int>("Id")
                    .ValueGeneratedOnAdd();

                b.Property<int?>("QuestionId");

                b.Property<string>("Text");

                b.HasKey("Id");

                b.HasIndex("QuestionId");

                b.HasAnnotation("Relational:TableName", "AnswerOption");
            });

the difference being that it initially uses b.ToTable("AnswerOption"); but after the migrate/remove migrate uses b.HasAnnotation("Relational:TableName", "AnswerOption");

if HasAnnotation is supposed to mean that my entity has that annotation in its definition, then it is worth noting that it does not have any attributes at all (on the class or any properties)

Again, hasn't been a problem for me, but seemed worth mentioning. (using RC2-16649)

esargent avatar Jan 22 '16 23:01 esargent

Interesting observation. Though it will not change anything functionally. ToTable() call is just a pretty way which essentially puts the annotation on the entity type. So in both cases, having ToTable() call or HasAnnotation() call, the model is identical. EF annotations and attributes are different things. EF annotation is a way to put extra metadata on the model which does not exist in the core. Especially provider specific metadata, like clustered index is SqlServer specific only hence it is not part of core metadata. TableName for an entity type is considered relational concept and therefore we put it as an annotation over entity type. HasAnnotation is function to put such EF annotation on the model object. Several fluent API calls like naming table/index/constraints etc. use annotations on lower level to store the metadata.

Why does the difference arise when removing migration? The runtime model has CLR type information for entity types. The model snapshot is fully in shadow state. When you add migration the model snapshot is written from runtime model which has CLR type information hence ToTable() is used to store the table name. When you remove migration the model to generate the snapshot comes from the previous migration's target model, which is in shadow state. That model does not have backing CLR types but it has annotations storing the table names from previous scaffolding therefore the model snapshot generated from this model is using HasAnnotation() instead of ToTable().

The only way it affects is merge conflicts in version control. @bricelam - do we want to make it more standard to avoid merge conflicts?

smitpatel avatar Jan 23 '16 04:01 smitpatel

You're using the nightly builds, right? I suspect the migrations were generated before we changed to always using ToTable and you reverted after. If you add an empty migration, the snapshot would also change. This is just a symptom of Migrations code churn.

bricelam avatar Jan 23 '16 04:01 bricelam

@smitpatel Thank you for the detailed explanation. One reason for participating in this form and with such early code is to understand what is happening under the covers better because it makes me a better developer.

@bricelam I'm using the RC2-16649 package, which is a few weeks old I think, but I think the feed is known as nightly build. I created the migration and reverted the same day - within about an hour of each other - on friday.

esargent avatar Jan 24 '16 05:01 esargent

@smitpatel we should just be consistent for RTM

rowanmiller avatar Feb 02 '16 19:02 rowanmiller

@bricelam @rowanmiller - Should we use to ToTable or HasAnnotation?

smitpatel avatar Feb 18 '16 18:02 smitpatel

If we have the time, I'd love to lift all relational annotations to their corresponding fluent API.

bricelam avatar Feb 18 '16 18:02 bricelam

That is actually a very good idea because HasAnnotation calls are the way metadata is stored but may confuse the users if they are not aware of it. Though that would require a reverse map for what annotation corresponds to which fluent API. Any thoughts on how to do that?

smitpatel avatar Feb 18 '16 18:02 smitpatel

If we have the time, I'd love to lift all relational annotations to their corresponding fluent API.

:+1:

rowanmiller avatar Feb 18 '16 18:02 rowanmiller

The "map" can just be logic in the model scaffolder. For example, when scaffolding a property look for the well-known relational (i.e. non-provider-specific) annotations like Relational:ColumnName, etc. Any unused annotations will still just get spit out as .HasAnnotation() calls.

bricelam avatar Feb 18 '16 19:02 bricelam

Few questions: We have metadata named DatabaseName but it doesn't have equivalent fluent API. Leave it as is? Should we convert Sequences which are stored as annotation after serializing?

smitpatel avatar Feb 23 '16 19:02 smitpatel