Code First Migration ignore DatabaseGeneratedOption? - entity-framework

I tried to change a field into a computed field.
That is, from:
Property(c=>c.IsPaid)
.IsRequired();
into:
Property(c=>c.IsPaid)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)
.IsRequired();
Then after add migration:
Add-Migration IsPaidAsComputed
Scaffolding migration gave me a migration class:
public partial class IsPaidAsComputed : DbMigration
{
public override void Up()
{
AlterColumn("dbo.Citations", "IsPaid", c => c.Boolean(nullable: false));
}
public override void Down()
{
AlterColumn("dbo.Citations", "IsPaid", c => c.Boolean(nullable: false));
}
}
Is Entity Framework not smart enough to detect what our intentioned change is?
What is the correct way to AlterColumn into a computed field?

Related

EF6: migration generates nullable string despite IsRequired

I'm trying to generate a migration for a new Entity class that has a string field, called "Name". That string field should not be nullable.
I'm aware that "nullable" and "non-empty" are two different issues (see EF 6 IsRequired() allowing empty strings ). But for now I just want to stick to non-null.
I'm failing to achieve that. The generated migration always ends up having "nullable:true", as shown :
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Tags",
columns: table => new
{
// ...
Name = table.Column<string>(nullable: true),
// ...
},
// ...
}
I'm aware that I could achieve this with a annotation ( EF6 Add-Migration for not nullable string? ) but our architect forbade those. I need to stick to the Configure function in IEntityTypeConfiguration.
I'm using IsRequired().
public class TagEntityTypeConfiguration : IEntityTypeConfiguration<Tag>
{
public void Configure(EntityTypeBuilder<Tag> builder)
{
builder.ToTable("Tag");
builder.HasKey(x => x.Id);
builder.Property(t => t.Name)
.IsRequired()
.HasMaxLength(100);
}
}
Why do I get nullable:true despite .IsRequired ?
I was simply forgetting to use the EntityTypeConfiguration :
modelBuilder.ApplyConfiguration(new TagEntityTypeConfiguration());
Without this line the constraints are ignored.

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.

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.

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