Changing Primary Key using Entity Framework Core Migrations - primary-key

I am trying to Change the Primary key of a table via Entity Framework Core Migrations:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_Permissions",
table: "Permissions");
}
When I try to update the database I get the following error message:
To change the IDENTITY property of a column, the column needs to be dropped and recreated.
How can I update the database?

I have found a solution:
This seems to be a bug in EF Core 1.1
I changed the Migration file.
More Information about this can be found here: https://thisworksonmymachine.com/2017/02/13/ef-core-the-setup-part-4/

It's always a risky idea to manipulate the migrations...
The simplest way is to clean the slate, if possible:
Take the table out from the DBContext (this will drop the table)
Create Migration and update database
Restore the table in the context and do your changes.
Create Migration and update database.
Obviously, you need to consider backup and restore of data and constraints...

On attempting to get IdentityServer4 EF migrations (SQLServer) I had this same problem. Updating Microsoft.EntityFrameworkCore to v2.1.3 and Microsoft.EntityFrameworkCore.Tools to v2.1.3 seemed to fix the issue.

The following is not a solution for everyone. Namely, if you have an established database in multiple environments.
If you don't care about your migrations, simply start from scratch by deleting all of your migrations. Then recreate the first one with your new schema. Should work just fine.

Related

Entity Framework update database

Using Entity Framework 6 (with code first approach), there is a exception if the database is not up to date
System.InvalidOperationException: "The model backing the 'xxx' context has changed since the database was created.
Is there a way to check, if the database is up to date and if not, update the database to the latest version from within the application itself?
Keeping your Database up to date with the EF Model is paramount.
For every change you make to the model of your context, there must be a migration to the database.
To maintain the integrity of the relationship between model and db you must:
Add-Migration migrationName_versionx.x.x
Update-Database
The exception you have experienced System.InvalidOperationException: is telling you that you that your model has not been migrated into your database. This means that EF is not able to work properly with possible queries you mave have.
Your database will keep a migration history which you can use to validate the integrity. If you are in doubt, simply apply a new migration and see if there were any changes.
This works using the DBMigrator Class
Dim migrator = New DbMigrator(new migrations.Configuration())
migrator.Update()

.NET Core database model changes in production

I follow code first approach to generate my databases by EFCore.
Now during development one of my models changed. How can I update the corresponding database tables on a live system at one of our customers? I do not want to dump or delete any data of course.
I'm still on .NET Core 1.1.0.
I fear you'll have to swallow the pill then.
Database.EnsureCreated() is only useful for developing, where you don't have to preserve data. If you have to preserve data or change the table schema, you have to use migrations or database scaffolding (create models from Database schema).
In any case, do a backup of the production data, before applying/attempting following steps and try it out upfront on a staging server.
One touchy option would be...
restore your code (i.e. going back to an older revision in your VCS)
create a table layout based on it (running Add-Migration InitialVersion or dotnet ef migrations add InitialVersion followed by Update-Database or dotnet ef database update
reapply your model changes (going back to your current revision in VCS, but keeping the files in the Migration folder)
run Add-Migration ModelUpdateV2 or dotnet ef migrations add ModelUpdateV2
run Script-Migration (Thanks #Smit!) or dotnet ef migrations script. This will generate an SQL command for applying the changes to the schema
From now on you have two options:
Look for a __EFMigrationHistory table within your database on the development database. It should contain one entry with your initial migration name. Export this table into your production system. Then you application should apply the migrations on the next start (when you call context.Database.MigrateAsync() within your Startup.cs).
From now on you should be able to use migrations in future
Just execute the migration script from above, without copying over __EFMigrationHistory, but then you'll have to repeat the steps above.
The other option is, to always create the models from the database (see dotnet ef dbcontext scaffold command (see EF Core Docs) do create models from database schema in future.
Then every time you change the table, you have to create your own SQL for migration and apply that before or while deploying the application.

Entity Framework migrations looking for deleted table

I am using EF6.
I created a migration which had some issues, because SQL had created a index that it knew nothing about. Trying to manually fix up what EF migrations had generated created more problems than it solved, so I decided to delete the entire table in SQL, then re-scaffold the migration.
The problem is that the new migration expects the table to be there. I expected migrations to know that it wasn't there, but this is obviously not how it works? Is that correct?
Is there a way to tell Migrations to look at the current DB and re scaffold from there?
Thanks
Depending on where you are in development, the easiest thing might be to just delete existing migrations and apply a new baseline using the -IgnoreChanges attribute (https://msdn.microsoft.com/en-us/data/dn579398.aspx#option1).
Otherwise, you can go through the generated Up() code and comment out lines that don't need to be applied. Then do Update-Database and your model will be back in sync.

Running Azure Mobile Server project locally

I'm running an Azure Mobile Server project locally with a local SQL Database.
I started from the Quickstart project, but as soon as I change the Entity to something more complex, I get this error with my migration:
"Cannot create more than one clustered index on table..."
Even with the correct schema I get this other error:
"Automatic migrations that affect the location of the migrations history system table (such as default schema changes) are not supported. Please use code-based migrations for operations that affect the location of the migrations history system table."
If you're using migrations, there's a chance that EF is trying to create both the primary key and CreatedAt column as clustered indexes. If you tell the migration to use the Azure Mobile Services/Apps SqlGenerator, it should do the right thing.
See if my answer here fixes the issue for you.
This is an artifact of Entity Framework, the ORM that .NET Mobile Services use. It's recommended that you use Code First Migrations to address this issue. Mobile Services has a full guide which is kept up to date on how to enable code first migrations. https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-how-to-use-code-first-migrations/
If you'd rather not deal with Code First Migrations, you can drop your database each time (not really a good idea for production purposes) or use the JavaScript backend.

EF - Create missing SQL Table/Object from Entity Object

Is there any function in the .NET Entity Framework, that can create missing SQL table. Or stored procedure or any other SQL Object?
Have do some research and did not found anything yet.
You could use code first migrations (either automatic or code based). If you are adding table to existing database create initial migration first and than add your new entity. All links are for EF 4.3 but the usage is exactly the same in EF 5.
Taken from here