I'm using Entity framework 5 code first in an Asp.Net MVC app. The app already created a table UserProfile. I moved the class in a different DbContext and also create some new classes for the DbContext; then I ran enabled-migration, add-migration and update-database. I got the following error when update-database.
There is already an object named 'UserProfile' in the database.
How to let EF-code-first don't generate the creating script for the table? However, I will need EF-code first to track the added columns in the class and update the table later.
Use Update-Database -Script for update a database manually. And you can exclude 'UserProfile' from resulting script.
Related
I am working on a new application in .Net 5 using EF Core. After creating some entity classes and doing the first few migrations I discovered that I wanted to change the data type of column and make it the key in one of the tables. I was able to do that without issue and the app works just fine with that change - but now if I try to change anything else in that table like add a new column and do a migration I get the following error: "To change the IDENTITY property of a column, the column needs to be dropped and recreated." I have tried even dropping the entire table - but nothing seems to work.
Whenever your migrations get messed up, especially early in a project, just delete the migrations folder, drop the Migration History table and start fresh with a new initial migration.
So I want to use migrations in a programmatic way, for this I've done something like this.
Seed is an extension method which I've created over StoreContext
StoreContext is my DbContext
internal StoreContext(DbContextOptions options)
: base(options)
{
RelationalDatabaseCreator creator = this.GetService<IDatabaseCreator>() as RelationalDatabaseCreator;
if (!creator.Exists())
{
creator.Create(); ///=> create database
creator.CreateTables(); ///=> create database tables
}
Database.Migrate(); ///=> apply migrations
if (creator.Exists())
{
this.Seed();
}
}
If the database does not exist the when I ran
Add-Migration Initial
all is good (this is only for my first migration)
If I decide to add new migration
Add-Migration Add_Student_FirstName
a new field in Student then I get this PMC exception
There is already an object named 'SomeTableName' in the database.
the SomeTableName that the console complains about is not necessarily the table on which I've added the change.
But if i go into my code and comment the line for
Database.Migrate(); ///=> apply migrations
and run again
Add-Migration Add_Student_FirstName
all is good again (my migration is added to Migrations folder, but I still have to do the migration by hand using the
Update-Database
command)
So what is wrong in what I'm doing, and how should I fix this so that the migration will be applied automatically when i create it ?
Thanks
I have an existing database.I am using code first from existing database.I am able to add new column to an existing table using add-migration and update database command.
But when I want to generate new table using a class new table is not created.
For example I want to create a new table LocaleResource.I have created a new class.
I have fired add-migration LocaleResourceClass and then Update database command.
But no new table is creating.I am using Entity Framework 6.1.
Please help me how to do that.
In order to have a migration created you have to add the new table to your context.
public virtual DbSet<LocalResource> LocalResources {get; set;}
Otherwise the migration macro does not know about your new table.
In case you are not sure where it was created just search your project for DbSet
Have an Order table with several foreign keys to a User table (different types of users).
The Order entity has multiple properties that represent the keys over to the User table.
The problem is these navigation properties get named User1, User2, User3 etc...
Is there a way to update the names of these properties and keep them intact when updating the datamodel?
For example, some times during development, if I make a change to this table, I will some times delete the table from the model, update and rebuild etc.
I think I will just have to manually rename these properties and remember to do this if I update the table and datamodel, or is there another way?
using db-migration feature helps you to do that. advantage is that it helps you to have an archive of changes you have made during development. for each change you have a class that represent changes happened to database.
after enabling the migration for your project :
first turn off automatic update
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
then use the code-first migration to generate migration class. write this in package manager :
add-migration renameColumns
then edit the created class to replace add/drop with rename.
public override void Up()
{
RenameColumn("Orders", "OldColumn", "NewColumn");
finally run update command in package manager to effect database
Update-Database
Now i'm using EF6 Alpha, and when using migration, it will add a new migration log into the __MigrationHistory table.
In EF6, The __MigrationHistory table has a new column called "ContextKey". After testing, I found there are two default "ContextKey" value:
The full name of DbContext's derived class.This happens when i run the code:
Database.CreateIfNotExists();
The full name of DbMigrationsConfiguration's derived class. This happens when i run the code:
public ArticleDbContext()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ArticleDbContext, ArticleConfiguration>());
}
The first time i run the application, "Database.CreateIfNotExists();" create a new database for me, also all tables that map to the models defined in ArticleDbContext, and then add a __MigrationHistory row which ContextKey's value is "Module.Article.Model.ArticleDbContext".
And then "Database.SetInitializer(new MigrateDatabaseToLatestVersion());" will be runned, this code will generate a new ContextKey "PowerEasy.Module.Article.Migrations.ArticleConfiguration". Migration query the __MigrationHistory table with this ContextKey and find out there's no data. So again it will create all tables that map to the models defined in ArticleDbContext, but the tables are already exist in the database, so an exception will be throwed, and tell me "the table XXX is already existed".
How can i solve this?
You should not mix Migrations and the Database.CreateIfNotExists method (or any of the initializers built on top of it). Migrations will take care of creating the database if it does not already exist.
As an alternative to the Migrations initializer, you can also apply migrations using the DbMigrator.Update method. This is useful if you want to create/update the database before it would otherwise be triggered by the initializer.