-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
DatabaseGeneratedOption.None Not Honored on ID Field When Published to Azure #6587
Comments
This problem isn't limited to publishing to Azure. I just recreated the database, via migrations within VS 2015, on my tablet and the same problem appeared. |
@markolbert could you provide a project (e.g. a console app) that repros this issue? |
Here you go. When I execute dotnet ef database update, the database (testdb) is created, all the migrations are successfully applied, and the ID field in County is an identity field, when it shouldn't be. Let me know if you need any more information. |
@markolbert I believe you are hitting this issue: #5345. Specifically, once a column has been created as an Identity column it is not possible to change that without rebuilding the table. Rebuilding the table is not something that Migrations can handle currently. In the current release the result is that a change to Identity is just ignored. In 1.1 Migrations will let you know that Migrations cannot handle the change--that is what #5345 was about. |
Ah! Yes, when I first defined the County table ID was autogenerated. I don't recall having to change that manually in the development database when I switched it to value-provided, but I must've forgotten. What I'm still confused by, though, is why it was a problem when the Azure database was created. Do the migrations get applied sequentially to the database, or are they "merged" together into one setup? I guess I assumed it was the latter, but it sounds like it's done sequentially. Which would definitely recreate the problem I ran into. |
@markolbert The migrations are applied sequentially. |
Good to know, thanx. I'll close this issue. |
This is under EF Core 1.0.
I have a simple class, County, whose integer ID is an input element, not auto-incremented:
When I use dotnet ef migrations add & dotnet ef database update locally, this correctly sets the ID field as non-nullable integer, but not an identity field (in SqlServer 2016).
However, when I published this app to Azure, the migration process ended up creating the field as non-nullable integer, but also as an identity field. This naturally caused problems when I tried to assign a value to the ID field when creating and saving a new instance of County.
As I am new to Azure I'm not sure this isn't something I caused myself, owing to how the migration was done. Basically, all I did, in a database seeding class I wrote, was to call:
_context.Database.Migrate();
And the entire database appeared to be built like magic, without me taking any other steps. Except that the ID field was defined wrong (it's the only DatabaseGeneratedOption.None field in my schema).
For fun, I tried using fluid design to reinforce the correct nature of that ID field (I left the annotation on the ID field while adding this next line to the OnModelCreating() method in the database context class):
builder.Entity<County>().Property( c => c.ID ).ValueGeneratedNever();
While this worked fine locally -- a new migration was created, and the local database was "updated" without any problems -- it did not solve the problem in the Azure database. To fix that, I had to go in via SSMS and manually edit the definition for the ID field. Not a big deal, but kind of annoying.
The text was updated successfully, but these errors were encountered: