Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique Constraint with Entity Framework using Code First Migrations

Tags:

I am doing entity framework (v 5) code first migrations, on an MVC 4 application. I would like to add a unique constraint at the database level.

I know this can be done when creating the table, but I already have a table. http://msdn.microsoft.com/en-US/data/jj591621

I have tried the following, marked as answer answer: Unique constraint with EFCodeFirst and SqlCe4

My database context differes slightly, I supply the connection name is as follows

public AppDatabaseContext() : base("MyConnectionDBContext")

When I use the Package Management Console to update the database, the overridden seed method is not called:

protected override void Seed(AppDatabaseContext context)

I have also tried the following: http://romiller.com/2010/07/31/ef-ctp4-tips-tricks-running-additional-ddl/

I did not use a nested class, this is because it seemed as if I had to registere the initializer via the app.config. I could not get it working while initializing it in code. The InitializeDatabase is called, but the following condition is never true:

(!context.Database.Exists() || !context.Database.ModelMatchesDatabase())

This is because this happens after the migrations have been run...

I also tried this at one stage: Unique Constraint in Entity Framework Code First, it was the same problem as before, this condition was never returning true.

Ideally, I would like to include some standard SQL in my migration file. Is there a way to do that? If not, where can I see how to achieve this with using code first migrations?

Thanks!

UPDATE:

Is there any reason why I can't use the SQL function?

 public override void Up()
 {
        AddColumn("Posts", "Abstract", c => c.String());

        Sql("UPDATE Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS NULL");
 }

Obviously using the correct SQL...

like image 233
Daryn Avatar asked Oct 25 '12 14:10

Daryn


People also ask

What is Code First Migrations in Entity Framework?

Code First Migrations is the recommended way to evolve your application's database schema if you are using the Code First workflow. Migrations provide a set of tools that allow: Create an initial database that works with your EF model. Generating migrations to keep track of changes you make to your EF model.

How do I enable Code First Migrations?

Go to Package Manager Console and type command help migration. Type Enable-Migrations -ContextTypeName EXPShopContext. This command creates a migration folder with InitialCreate. cs and Configuration.

How do I change the default value for EF core?

In EF core released 27th June 2016 you can use fluent API for setting default value. Go to ApplicationDbContext class, find/create the method name OnModelCreating and add the following fluent API.


1 Answers

With code first migrations, I've just used this in the Up() method to add a unique index on a single column:

CreateIndex(table: "Organisations", 
            column: "Name", 
            unique: true, // unique index
            name: "MyIndex");

...and then in the Down() method:

DropIndex(table: "Organisations", 
          name: "MyIndex");

Is that what you're after?

like image 143
MarkG Avatar answered Oct 11 '22 13:10

MarkG