Tag Archives: entityframework

Entity Framework Code First migrations and the [StringLength] annotation

Recently I needed to change my model so that a field would be checked for uniqueness. I eagerly added the [StringLength(3)] and [Index(IsUnique = true)] annotations to the model and ran Add-Migration and Update-Database. Close, but no cigar unfortunately. Update-Database kept throwing the following error: System.Data.SqlClient.SqlException (0x80131904): Column 'IsoCode' in table 'dbo.CurrencyModels' is of a type that is invalid for use as a key column in an index.

This is due to the fact that the generated code migration, was only applying the index and not the length restriction. You can fix this directly in the Up() and Down() methods of the code migration by using AlterColumn() as follows:

    public partial class UniqueCurrency : DbMigration
    {
        public override void Up()
        {
            AlterColumn("dbo.CurrencyModels", "IsoCode", c => c.String(maxLength: 3));
            CreateIndex("dbo.CurrencyModels", "IsoCode", unique: true);
        }
        
        public override void Down()
        {
            AlterColumn("dbo.CurrencyModels", "IsoCode", c => c.String(maxLength: null));
            DropIndex("dbo.CurrencyModels", new[] { "IsoCode" });
        }
    }

Entity Framework: How to delete your old database and start fresh with a new one

In a previous post I explained how to recover after deleting a Entity Framework database. In this post we’ll see the proper way to recreate the database in Entity Framework:

Careful

before you start, make sure you’ve got source-control or back-ups of the code in the Migrations folder. When you delete the Migrations folder, you’re deleting code that:

  1. seeds the database with the initial set of data
  2. and up/downgrades the database to the various versions

In Visual Studio:

  1. Go to Server Explorer, right-click on the data collection that represents your context and choose delete.
  2. Go to Solution Explorer and delete the .mdf file. You might have to click on the “Show All Files” icon before you see it.
  3. Go to the Solution Explorer and delete the Migrations folder.

At this point, you have a solution that will create a new database when its run. If you need to seed the database or expect your models to change, then you’ll want to do the following:

  1. Tell EF to create a fresh database bases on the current models by going to the Package Manager Console and running the following commands:
    Enable-Migrations
    Add-Migration Initial
    Update-Database
    
  2. Update the code in Migrations\Configuration.cs to seed the database with the data you need.

Entity Framework and the error: Cannot attach the file ‘xxx.mdf’ as database ‘xxx’

Say you’re working on a project that’s using Entity Framework to manage the database storage in a SQL Server Express installation. If you delete the .mdf file you’ll keep on getting the error” Cannot attach the file 'xxx.mdf' as database 'xxx'.

To solve it, in visual studio go to the Package Manager console and run the following commands:

sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0
Update-Database