Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter Identity is always added to migrations #20971

Closed
overdravila opened this issue May 15, 2020 · 19 comments
Closed

Alter Identity is always added to migrations #20971

overdravila opened this issue May 15, 2020 · 19 comments

Comments

@overdravila
Copy link

overdravila commented May 15, 2020

Every time I create a migration, it adds an AlterColumn statement to change the Identity column of a property, which i have not modified. If you then try to do Update Database, it will fail, since it needs to recreate the table, and Entity Framework does not support that. Also, dropping the table is not an option, since I have data there which I can't remove. It always happen with the same entity, but it has happened with several different ones in the past. In order for me to be able to update the database with any migration, I need to manually remove that piece of code from the migration itself.

Steps to reproduce

Add a new migration

Code snippets

Generated migration file
image

Entity
image

DbContext
image
image

Exception

image

Further technical details

EF Core version: 3.1.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.1
Operating system: Windows 10
IDE: VS Code

@ajcvickers
Copy link
Contributor

@andresargos Is it your intention to generate an Identity column for PersonTypeID? (The code has ValueGeneratedOnAdd which would indicate an Identity column, but a fixed set of values in the enum, which would be unusual for an Identity column.

@overdravila
Copy link
Author

Yes, I want to generate an identity column for PersonTypeID. I want it to be auto incremental if I add a new record on the database, but I want it to start with the data from those enums. However, this also happens randomly sometimes for other tables, where it adds an AlterColumn for an identity field I am not touching.

@ajcvickers
Copy link
Contributor

@andresargos I have not been able to reproduce this. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

@overdravila
Copy link
Author

How should I post this? Is a link to a folder in drive okay?

@ajcvickers
Copy link
Contributor

@andresargos Generally people just attach the small, runnable project to this issue.

@overdravila
Copy link
Author

@ajcvickers Great, I am keeping my EF context and models in a separate project to the rest of the web api, so here's the complete EF project I am working on. I think you may need to change the context connection string.

ExpressMapApi.Data.zip

@ajcvickers
Copy link
Contributor

I was able to reproduce this (on 3.1.4 and 5.0.0 preview 4). It appears to be caused by an enum property mapped to an Identity column.

@overdravila
Copy link
Author

So, the recommendation would be to not use enumerations mapped on identity columns, correct? Do you think it doesn't make any sense to set that column as Identity?

@ajcvickers
Copy link
Contributor

@andresargos This is a bug--it should work. As a workaround I would just delete the unneeded changes in the generated migrations.

@overdravila
Copy link
Author

Yeah, that's what I've kept doing. Hope it gets fixed.

Thanks!

@smitpatel
Copy link
Contributor

Just wondering why am I assigned to this issue @ajcvickers ?

@bricelam
Copy link
Contributor

Note, it is not currently possible to mark an enum property as IDENTITY.

modelBuilder.Entity<PersonType>().Property(r => r.PersonTypeID).UseIdentityColumn();

Identity value generation cannot be used for the property 'PersonTypeID' on entity type 'PersonType' because the property type is 'PersonTypes'. Identity value generation can only be used with signed integer properties.

@bricelam bricelam removed this from the 5.0.0 milestone Aug 25, 2020
@ajcvickers
Copy link
Contributor

@bricelam What happens if you just mark it as ValueGenerated.OnAdd?

@bricelam
Copy link
Contributor

A non-IDENTITY column is created. Not sure what SaveChanges does.

@bricelam
Copy link
Contributor

But, there is a bug here in that we don't mark the property in the snapshot as non-IDENTITY. We need to do this because when we use the underlying type in the snapshot, ValueGenerated.OnAdd suddenly means IDENTITY which is why we get a diff every time.

@bricelam
Copy link
Contributor

I'll update the snapshot to look like this (note, there's no provider-specific Fluent API for this)

b.Property<int>("PersonTypeID")
    .ValueGeneratedOnAdd()
    .HasColumnType("int")
    .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.None);

@bricelam
Copy link
Contributor

In triage, let's discuss whether we should enable enum IDENTITY properties.

bricelam added a commit to bricelam/efcore that referenced this issue Aug 25, 2020
Synthesizes the annotation during type erasure

Part of dotnet#20971
bricelam added a commit to bricelam/efcore that referenced this issue Aug 26, 2020
Synthesizes the annotation during type erasure

Part of dotnet#20971
bricelam added a commit to bricelam/efcore that referenced this issue Aug 27, 2020
bricelam added a commit to bricelam/efcore that referenced this issue Aug 27, 2020
@bricelam bricelam removed their assignment Aug 28, 2020
@JeremyLikness JeremyLikness added this to the Backlog milestone Sep 4, 2020
@JeremyLikness
Copy link
Member

@roji ping to track possible duplicate

@bricelam
Copy link
Contributor

bricelam commented Sep 8, 2020

Duplicate of #11970

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants