I have an existing database that was created with code-first approach
Now I'd want to add an another property to my model, but after running app and trying to work on that data EF Core says that
System.Data.SqlClient.SqlException: 'Invalid column name 'test'.'
That's reasonable, so I'm removing that property (test), creating migration, adding that property (test) once again, creating another migration and updating model
But now:
There is already an object named 'AspNetRoles' in the database.
How can I extend my db schema with code first without losing my data?
Related
I am currently trying to use EF Core Migrations to remove the table and entity type from the database. (Let's say I've got an Entity Test and I've decided to move it to a different database/service. Entity Test was added to the EF/Database using migrations.
At first after deleting the DbSet from DbContext and removing all references to Test from other types I was not able to generate migration - I was getting a duplicate key exception. After Googling for a bit I've found an answer which was telling me to first Empty the Test class and remove all references -> Generate Migration -> Remove Test -> Generate Migration - the presented approach resulted in generation of 2 working migrations which was fine.
The problem is that I currently can't apply those migrations to database. Every attempt to execute migration on empty database ends up throwing : There is no entity type mapped to the table 'Test' which is used in a data operation. Either add the corresponding entity type to the model, or specify the column types in the data operation.
Is it not possible to delete once added entity type from the application if it as ever used in EF migrations - or am I doing something terribly wrong - cuz honestly I find it hard to believe that EF wouldn't allow for deletion of entities/tables.
[EF Core Version 6.0.3]
Well there are "references" present to the type in historic migrations like
modelBuilder.Entity("Bleh.Customer.Domain.Model.Test, b => "
but does it mean that I've to somehow modify each historic migration?
I am working on asp.net core and using EF. I have created database from scratch using EF. Now whenever there are changes in the database, adding new columns, or changing the type etc. Then I run the Update-datebase migration but I am getting an error for having tables and other object in database. Can someone help me what changes would I need to make in order to make the migration successful?
The compile error mentioned that the object already exists. Lets say if I have customer table that is already created in the database, when I run the script again after adding new object or modification, I am getting that error, this does make sense but what is the common practice to deal with the issue?
As described in Migrations Overview after you are evolving your models (adding properties, removing properties, editing them and etc...) you need to update your database schema, in order to do that, you are required to add a new migration.
This can be possible in the one of following ways:
via .NET CLI
dotnet ef migrations add NewMigrationName
via PowerShell
Add-Migration NewMigrationName
After adding a new migration you just need to sync the database by:
Update-Database
I'm trying to create a DbContext specific to may Domain that has one model class entirely managed by EF, and another that gets mapped to a database view. To ensure EF doesn't try to create this view that already exists, I added just the view to my DbContext and ran the following command:
Add-Migration InitialUnmanaged -IngoreChanges
This creates a Migration with an empty Up and Down, which is exactly what I want. I'm able to update my dev database using this Migration, but whenever I try to add a second Migration that includes my EF-managed model class, I get an error.
Add-Migration Initial
Unable to generate an explicit migration because the following explicit migrations are pending: [201510151553565_InitialUnmanaged]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
The thing is, I've already applied that explicit Migration, and I can see it in my MigrationHistory table.
MigrationId
201510151553565_InitialUnmanaged
Can anyone help me understand why this is happening and how to work around it?
I figured this out.
When I applied the Migration, it put the Migration History table in the same default schema as the model. However, when I tried to roll it back, it was looking for the Migration History in the connected user's schema. Since it wasn't there, it thought the database was at version 0.
I created an OracleDbConfiguration class to specify a custom HistoryContext that specifies the default schema for the Migration History table and I'm able to rollback as expected after applying a Migration.
I verified that I was able to recreate my initial steps and have them work as expected now that I'm explicitly specifying the schema for the Migration History table.
Either delete the migration or just run Update-Database to put entity back in sync. If it is an empty migration your database will not change.
I am on a project that has an EF model that was created with a schema of 'dbo'. At some point in time the schema was changed to something more meaningful. However, try as I may I cannot now update from the database with the newly named schema as all the entities create new objects instead of updating the current ones. For example instead of Person, I now get and Person1. I have tried to edit connection strings as well as the EDMX file but nothing seems to be working.
Is there anyway to get my model to update from the database with the renamed schema?
I am using EF 6.0 and .Net 4.5.
I'm building an application that will be exposed to the public. So the database will be filled with data. I'm not sure how to extend the model afterwards. Locally after any changes of the model the database get's recreated. How can add properties to the existing model, without recreate the entire database ?
Take a look at EntityFramework CodeFirst Migration, for modifying your schema.
MSDN Link for Migrations