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