Entity framework migration how to seed data from previous table - entity-framework

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");
}

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

Stop empty strings at the database level with EF code first

Consider the following POCO entity for Entity Framework Code First:
public class Foo
{
public int Id { get; set; }
[Required, StringLength(100)]
public string Name { get; set; }
}
Which will generate the following table:
CREATE TABLE [dbo].[Foo] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
CONSTRAINT [PK_dbo.Foo] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Now, I understand that the default behavior of EF is to convert empty strings to null. So even if I explicitly feed it an empty string I will get a validation exception, which is perfect. The following code will throw a DbEntityValidationException:
var f = new Foo { Name = "" };
context.Foos.Add(f);
context.SaveChanges();
But, the problem is if I have an external application which accesses the database directly, I can perform the following query and it succeeds:
insert into dbo.Foo(Name)
values ('')
The best solution is arguably to not allow anyone to connect directly to the database and force them through a business layer. In reality however this may not always be possible. Especially if, say, I myself am importing external data via an SSIS package.
My best understanding says that applications should be set up to reject as much bad data at the lowest level possible. In this case this would mean the at database level. So if were creating the database the old fashioned way, I would add a constraint to check (Name <> '') and stop dirty data from ever being inserted in the first place.
Is there a way to get EF Code First to generate this constraint for me, or some other way to get it to enforce a non-empty-string (minimum length 1) at the database level - preferably using an attribute? Or is my only recourse to add the constraint manually in a migration?
There is MinLength attribute but it does not enforce the constraint on database level, you should add this constraint using migration I think.
public partial class test : DbMigration
{
public override void Up()
{
Sql("ALTER TABLE [dbo].[YOUR_TABLE] ADD CONSTRAINT " +
"[MinLengthConstraint] CHECK (DATALENGTH([your_column]) > 0)");
}
public override void Down()
{
Sql("ALTER TABLE [dbo].[YOUR_TABLE] DROP CONSTRAINT [MinLengthConstraint]");
}
}
You can add sql code generators for EF to generate these codes for MinLength attribute, I'll give you a simplified hint here:
First mark properties with MinLength
public class Test
{
public int Id { get; set; }
[MinLength(1)]
public string Name { get; set; }
}
Add MinLenghtAttribute to conventions and provide the value, which is the Length :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention<MinLengthAttribute, int>(
"MinLength",
(property, attributes) => attributes.Single().Length));
}
the generated code for migration will be:
CreateTable(
"dbo.Tests",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(
annotations: new Dictionary<string, AnnotationValues>
{
{
"MinLength",
new AnnotationValues(oldValue: null, newValue: "1")
},
}),
})
.PrimaryKey(t => t.Id);
Override the SqlServerMigrationSqlGenerator to use this convention in order to generate the constraint sql code:
public class ExtendedSqlGenerator : SqlServerMigrationSqlGenerator
{
protected override void Generate(AddColumnOperation addColumnOperation)
{
base.Generate(addColumnOperation);
AddConstraint(addColumnOperation.Column, addColumnOperation.Table);
}
protected override void Generate(CreateTableOperation createTableOperation)
{
base.Generate(createTableOperation);
foreach (var col in createTableOperation.Columns)
AddConstraint(col, createTableOperation.Name);
}
private void AddConstraint(ColumnModel column, string tableName)
{
AnnotationValues values;
if (column.Annotations.TryGetValue("MinLength", out values))
{
var sql = string.Format("ALTER TABLE {0} ADD CONSTRAINT " +
"[MinLengthConstraint] CHECK (DATALENGTH([{1}]) >= {2})"
,tableName, column.Name, values.NewValue);
Generate(new SqlOperation(sql));
}
}
}
the code above contains generation for AddColumn and CreateTable operations you must add codes for AlterColumn, DropTable and DropColumns as well.
Register the new code generator:
internal sealed class Configuration : DbMigrationsConfiguration<TestContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
SetSqlGenerator("System.Data.SqlClient", new ExtendedSqlGenerator());
}
}

Entity Framework Migrations: Prevent dropping column or table

If EF migration decides to rename a column (or a table), it fulfills this by dropping the (old) column and then adding a column with the new name. This clearly leads to data loss.
Is there any way to prevent EF migration from dropping a column and force it to use RenameColumn instead?
Well, I didn't find a clear and straightforward solution.
My solution is hiding DbMigration class which every migration that was generated using code-based migration is derived from. I do this by introducing a new class with the same name (DbMigration). Then I put it inside the same assembly and same namespace where code files reside. This way, any reference of code files to the original DbMigration resolves to the new DbMigration type. Then, I can switch dropping a column ON or Off:
namespace MyProject.DAL.Migrations
{
/// <summary>
/// Customized DbMigration which protects columns to be dropped accidentally
/// </summary>
public abstract class DbMigration : global::System.Data.Entity.Migrations.DbMigration
{
public bool AlloDropColumn { get; set; }
protected internal new void DropColumn(string table, string name, object anonymousArguments = null)
{
if (!AlloDropColumn)
throw new Exception("MyProject: Dropping a column while updating database is prohibited. If you really want to drop column(s), set property 'AllowDropColumn' true.");
}
}
}
And in a code file:
public partial class _1 : DbMigration
{
public override void Up()
{
AlloDropColumn = true; // To allow column drop
AddColumn("driver.TruckDriver", "FullName2", c => c.String());
DropColumn("driver.TruckDriver", "FullName");
}
public override void Down()
{
AddColumn("driver.TruckDriver", "FullName", c => c.String());
DropColumn("driver.TruckDriver", "FullName2");
}
}

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.

Is it possible to append indexes modifying initial migration?

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.