Is it possible to append indexes modifying initial migration? - entity-framework

I'm using entity framework code first approach in my project. I've created model and enabled migrations using enable-migrations statement in package manager console. My context class contains mapping configurations and relationships like
modelBuilder.Configurations.Add(new PostMap());
modelBuilder.Configurations.Add(new UserMap());
Up method in my migration file 201302261054023_InitialCreate.cs contains all table definitions as my context specified via DbSet<Type> property.
I changed InitializeDatabase method in my custom database Initializer, because I want to migrate up to specific migration during database initialization.
public void InitializeDatabase(T context)
{
bool databaseExists = context.Database.Exists();
if (!databaseExists)
context.Database.CreateIfNotExists();
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update("InitialCreate");
}
Is possible to modify Up method in 201302261054023_InitialCreate class to add another index from
CreateTable(
"dbo.City",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 450),
})
.PrimaryKey(t => t.Id);
to
CreateTable(
"dbo.City",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 450),
})
.PrimaryKey(t => t.Id)
.Index(t=>t.Name,true);
without appending another migration?

After mining plenty of web pages related to migrations, I've found that modifying initial migration could help me only if I update database to "zero migration" (empty database). Without migration to zero database I could add another migration and create indexes in new migration like
public partial class InitialIndexes : DbMigration
{
public override void Up()
{
CreateIndex("City", "Name", true);
}
public override void Down()
{
DropIndex("City", new[] {"Name"});
}
}

To create an index you can use the ExecuteSqlCommand.
context.Database.ExecuteSqlCommand("CREATE INDEX IX_TableName_ColumnName ON TableName (ColumnName)");
I normally have this command in a Seed override method like the following.
protected override void Seed(YourContext context)
{
context.Database.ExecuteSqlCommand("CREATE INDEX IX_TableName_ColumnName ON TableName (ColumnName)");
//...Rest of seed code
}
You should be able to use the same call in your method.

Related

dependency on column EF6

I get an error when I try to do a migration
The object 'DF_dbo.dbMetrics_metricName' is dependent on column 'metricName'.
ALTER TABLE ALTER COLUMN metricName failed because one or more objects access this column.
I am trying to change it from a GUID to a string as I want to be able to manually add my ID's in.
public override void Up()
{
DropPrimaryKey("dbo.dbMetrics");
AlterColumn("dbo.dbMetrics", "metricName", c => c.String(nullable: false, maxLength: 128));
AddPrimaryKey("dbo.dbMetrics", "metricName");
}
public override void Down()
{
DropPrimaryKey("dbo.dbMetrics");
AlterColumn("dbo.dbMetrics", "metricName", c => c.Guid(nullable: false, identity: true));
AddPrimaryKey("dbo.dbMetrics", "metricName");
}
Here is my migration class file

How should I update a newly added column in Entity Framework Code First Migration

I am learning EF Code First Migrations. I have added a new column to an exiting table
public partial class Test2 : DbMigration
{
public override void Up()
{
AddColumn("dbo.Recipes", "DeafultNumberOfServes", c => c.Int(nullable: false));
}
public override void Down()
{
DropColumn("dbo.Recipes", "DeafultNumberOfServes");
}
}
I want to now update the existing rows in that table and set the DeafultNumberOfServes to 4
It works in the Seed method of my Configuration class but it feels dodgy
protected override void Seed(MenuPlannerDBContext context)
{
context.Recipes.AddOrUpdate(
new Recipe { RecipeID = 1, Description = "Recipe 1" },
new Recipe { RecipeID = 2, Description = "Recipe 2" },
new Recipe { RecipeID = 3, Description = "Recipe 3" }
);
var result = context.Recipes.Where(x => x.DeafultNumberOfServes == 0).ToList();
result.ForEach(x => x.DeafultNumberOfServes = 4);
context.SaveChanges();
}
I have also tried to put it in the Migration class itself but it is as if the sql never get run and all the rows contain the default 0 in that column.
public partial class Test2 : DbMigration
{
public override void Up()
{
AddColumn("dbo.Recipes", "DeafultNumberOfServes", c => c.Int(nullable: false));
Sql("UPDATE dbo.Recipes SET DeafultNumberOfServes = 4");
}
public override void Down()
{
DropColumn("dbo.Recipes", "DeafultNumberOfServes");
}
}
So what would be the best way to handle adding a column and then updating all existing rows to a value?
Reading between the lines, it looks like you want the DeafultNumberOfServes property to default to 4 rather than 0 if the user hasn't specified it. You can specify the database default by changing the code in the Up migration to this:
public override void Up()
{
AddColumn("dbo.Recipes",
"DeafultNumberOfServes",
c => c.Int(nullable: false, defaultValue: 4));
}
This is mainly useful when you have existing data and want to add a non-nullable column without hitting an error. It doesn't mean that the value will default to 4 if the user doesn't specify it. That is because EF will send the default value for an int (i.e. zero) unless you set it in code, and that will overwrite the database value.
So you need to set the default in code. You do that by specifying it in the constructor or in a backing field (which is my preference):
public class Recipe
{
private _DefaultNumberOfServes = 4;
public int DeafultNumberOfServes
{
get{ return _DefaultNumberOfServes;}
set{ _DefaultNumberOfServes = value}
}
}
BTW, the reason your migration looks like it isn't working is because the migration runs on an empty table before your seed - so there is nothing to update - the table is empty because you are using an initialiizer that is "dropping and recreating the database always". I would advise changing to a "migrate to latest version" initializer instead so that you get into the pitfalls of migrating to a database with existing data sooner rather than later.

Entity framework migration how to seed data from previous table

I have a column of type time that I need to change to type bigint.
As I see it, the only way is to drop the column and create a new one with a bigint type.
For that I will run the following migration :
public partial class Migration1 : DbMigration
{
public override void Up()
{
DropColumn("dbo.MyDurations", "Duration");
AddColumn("dbo.MyDurations", "Duration", c => c.Long(nullable: false));
}
public override void Down()
{
DropColumn("dbo.MyDurations", "Duration");
AddColumn("dbo.MyDurations", "Duration", c => c.Time(nullable: false, precision: 7));
}
}
How can I get the data from the current column in time type and seed it to the new one by transforming it with TimeSpan.Ticks(duration) ?
From what I understood, I can only seed data from the Seed(DbContext ctx) function from the Configuration file. But the seed method is run after the migration.
Is it possible to access data before applying migration, then apply the migration and then seed the data ?
You can run your own Sql in a migration using the Sql method. You need to change your migration to something like this:
public override void Up()
{
AddColumn("dbo.MyDurations", "NewDuration", c => c.Long(nullable: false));
Sql("UPDATE dbo.MyDurations SET NewDuration = Duration");
DropColumn("dbo.MyDurations", "Duration");
RenameColumn("dbo.MyDurations", "NewDuration", "Duration");
}

What is the difference between EntityTypeConfiguration and DbMigration for a new EF project

Should I use EntityTypeConfiguration to build my data model, or should I directly enable migrations an use DbMigration.
That is use :
public class BlogEFCFConfiguration : EntityTypeConfiguration<Blog> {
public BlogEFCFConfiguration()
: base() {
HasKey(x => x.BlogId);
Property(x => x.BlogId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.Name).HasColumnType("varchar").HasMaxLength(128);
}
}
or
public partial class InitialCreate : DbMigration {
public override void Up() {
CreateTable(
"dbo.Blogs",
c => new
{
BlogId = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 128, unicode: false),
})
.PrimaryKey(t => t.BlogId);
}
public override void Down() {
DropTable("dbo.Blogs");
}
}
Indeed if I want to change my model I will have to finally use DbMigration. So why use EntityTypeConfiguration ?
It is may be a too open question.
They are doing different jobs - EF migrations ensure that the application and database changes are aligned. The configurations inform EF how to map from your object model to your relational model.
Configurations are required when the default conventions don't suit your model.
EF migrations are required if you wish to model the database changes in code between application versions. This has the advantage of being able to automatically have the application update the database to the latest version on startup for example.

How can I stop Entity Framework 5 migrations adding dbo. into key names?

I started a project using Entity Framework 4.3 Code First with manual migrations and SQL Express 2008 and recently updated to EF5 (in VS 2010) and noticed that now when I change something like a foreign key constraint, the migrations code adds the "dbo." to the start of the table name and hence the foreign key name it constructs is incorrect for existing constraints (and in general now seem oddly named).
Original migration script in EF 4.3 (note ForeignKey("Products", t => t.Product_Id)):
CreateTable(
"Products",
c => new
{
Id = c.Int(nullable: false, identity: true),
ProductName = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"KitComponents",
c => new
{
Id = c.Int(nullable: false, identity: true),
Component_Id = c.Int(),
Product_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("Products", t => t.Component_Id)
.ForeignKey("Products", t => t.Product_Id)
.Index(t => t.Component_Id)
.Index(t => t.Product_Id);
Foreign Key names generated:
FK_KitComponents_Products_Product_Id
FK_KitComponents_Products_Component_Id
If I then upgrade to EF5 and change the foreign key the migration code looks something like (note the "dbo.KitComponents" and "dbo.Products" as opposed to just "KitComponents" and "Products"):
DropForeignKey("dbo.KitComponents", "Product_Id", "dbo.Products");
DropIndex("dbo.KitComponents", new[] { "Product_Id" });
and the update-database fails with the message:
'FK_dbo.KitComponents_dbo.Products_Product_Id' is not a constraint.
Could not drop constraint. See previous errors.
so it seems as of EF5 the constraint naming has changed from
FK_KitComponents_Products_Product_Id
to
FK_dbo.KitComponents_dbo.Products_Product_Id (with dbo. prefix)
How can I get EF5 to behave as it was in EF 4.3 so I don't have to alter every piece of new migration code it spits out?
I haven't been able to find any release notes about why this changed and what to do about it :(
You can customize the generated code by sub-classing the CSharpMigrationCodeGenerator class:
class MyCodeGenerator : CSharpMigrationCodeGenerator
{
protected override void Generate(
DropIndexOperation dropIndexOperation, IndentedTextWriter writer)
{
dropIndexOperation.Table = StripDbo(dropIndexOperation.Table);
base.Generate(dropIndexOperation, writer);
}
// TODO: Override other Generate overloads that involve table names
private string StripDbo(string table)
{
if (table.StartsWith("dbo."))
{
return table.Substring(4);
}
return table;
}
}
Then set it in your migrations configuration class:
class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
CodeGenerator = new MyCodeGenerator();
}
}
For Automatic Migrations use this code:
public class MyOwnMySqlMigrationSqlGenerator : MySqlMigrationSqlGenerator
{
protected override MigrationStatement Generate(AddForeignKeyOperation addForeignKeyOperation)
{
addForeignKeyOperation.PrincipalTable = addForeignKeyOperation.PrincipalTable.Replace("dbo.", "");
addForeignKeyOperation.DependentTable = addForeignKeyOperation.DependentTable.Replace("dbo.", "");
MigrationStatement ms = base.Generate(addForeignKeyOperation);
return ms;
}
}
And Set it on configuration:
SetSqlGenerator("MySql.Data.MySqlClient", new MyOwnMySqlMigrationSqlGenerator());
This is a fine answer, however, if you're just looking for a 'quick fix' approach, there's this as well EF Migrations DropForeignKey fails when key is in a base class
Use the DropForeignKey overload which contains the parameters principalName and name -- which in this case means constraint name!
Improving on bricelam's answer, I tried this on EF6. Made a few changes to keep the schema as part of table name and only remove it from the FK or PK name
internal class MyCodeGenerator : CSharpMigrationCodeGenerator
{
protected override void Generate(AddForeignKeyOperation addForeignKeyOperation, IndentedTextWriter writer)
{
addForeignKeyOperation.Name = this.StripDbo(addForeignKeyOperation.Name, addForeignKeyOperation.DependentTable);
addForeignKeyOperation.Name = this.StripDbo(addForeignKeyOperation.Name, addForeignKeyOperation.PrincipalTable);
base.Generate(addForeignKeyOperation, writer);
}
protected override void Generate(AddPrimaryKeyOperation addPrimaryKeyOperation, IndentedTextWriter writer)
{
addPrimaryKeyOperation.Name = StripDbo(addPrimaryKeyOperation.Name, addPrimaryKeyOperation.Table);
base.Generate(addPrimaryKeyOperation, writer);
}
protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation, IndentedTextWriter writer)
{
dropForeignKeyOperation.Name = this.StripDbo(dropForeignKeyOperation.Name, dropForeignKeyOperation.DependentTable);
dropForeignKeyOperation.Name = this.StripDbo(dropForeignKeyOperation.Name, dropForeignKeyOperation.PrincipalTable);
base.Generate(dropForeignKeyOperation, writer);
}
protected override void Generate(DropPrimaryKeyOperation dropPrimaryKeyOperation, IndentedTextWriter writer)
{
dropPrimaryKeyOperation.Name = StripDbo(dropPrimaryKeyOperation.Name, dropPrimaryKeyOperation.Table);
base.Generate(dropPrimaryKeyOperation, writer);
}
private string StripDbo(string objectName, string tableName)
{
if (tableName.StartsWith("dbo."))
{
return objectName.Replace(tableName, tableName.Substring(4));
}
return objectName;
}
}