Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:26:34+00:00 2026-06-16T17:26:34+00:00

I am doing entity framework (v 5) code first migrations, on an MVC 4

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:26:36+00:00Added an answer on June 16, 2026 at 5:26 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing code-first development with Entity Framework 4.3 and it doesn't seem like it's
Doing Entity Framework (5) code first, although we are not generating our data model
I have an application which uses Entity Framework Code First models. The structure of
I am using entity framework 4.1 and code first in asp.net mvc. Just to
I'm using Entity Framework 4.2 (Code First) to access my database. I was under
Hi I am using Entity framework code first for the first time and I
I've created a web application project in Visual Studio 2010 using Entity Framework Code
I am writing code first Entity Framework app using POCO. Here is simplified version:
I wish to use Entity Framework Code-first for a new project. So i decided
I would like to execute queries in Entity Framework that use Local tracked entities

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.