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
Related
I have an entity for which I don't want a table. The entity is used to generate a report and is populated using a SQL query:
public IEnumerable<Entities.MembersReportRow> Get()
{
return _context.MembersReportRows
.FromSqlRaw(#"
SELECT...")
.ToList();
}
Using modelBuilder.Ignore<MembersReportRow>(); stops the code for creating the table from being added to the migration, but then the method above stops working with error:
Cannot create a DbSet for 'MembersReportRow' because this type is not
included in the model for the context.
I've also tried:
modelBuilder.
.HasNoKey()
.ToSqlQuery(#"
SELECT...
");
...but the code to create the table still gets created in the migration, which surprises me a little as I'm specifically stating "ToSqlQuery".
Any help would be appreciated!
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
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.
I created POCO classes and then EF created the database tables for me when I tried to access the data. This worked without problem. I have now populated my tables with data. Not just seed data but real data.
Now I would like to add another column to a table. I assume the first thing I need to do is to add a field to the POCO class but what's next after that? I now have my database filled with data. On the SQL side I know how to add the column myself but do I have to do something with EF or will it automatically pick up that my column was added to the table and my field to the POCO class?
You can use Code First Migrations (http://msdn.microsoft.com/en-us/library/hh770484(v=vs.103).aspx). It can update your database automatically or not.
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.