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

  • Home
  • SEARCH
  • 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 8025631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:13:39+00:00 2026-06-04T23:13:39+00:00

When adding a new non-nullable column to a table using code first migrations, it

  • 0

When adding a new non-nullable column to a table using code first migrations, it will automatically create a default value for you. This makes sense because existing rows need to have a value for the new column (since it can’t be null). That’s fine, but after that value is assigned everywhere, I don’t want it anymore. It’s non-nullable, because I want to make sure that a value is always explicitly inserted. If everything defaults to ” or 0 then I’ll have magic strings. So anyway, I can up with a solution, that I’m not thrilled of, but it works.

After adding the column, I then drop the default constraint.

public override void Up()
{
    AddColumn("dbo.SomeTable", "NewColumn", c => c.Int(nullable: false));
    Sql(Helpers.DropDefaultConstraint("dbo.SomeTable", "NewColumn"));
}

…

public static string DropDefaultConstraint(string table, string column)
{
    return string.Format(@"
        DECLARE @name sysname

        SELECT @name = dc.name
        FROM sys.columns c
        JOIN sys.default_constraints dc ON dc.object_id = c.default_object_id
        WHERE c.object_id = OBJECT_ID('{0}')
        AND c.name = '{1}'

        IF @name IS NOT NULL
            EXECUTE ('ALTER TABLE {0} DROP CONSTRAINT ' + @name)
        ",
        table, column);
}

PROS: Once the helper method is implemented, I just need to add one simple line to drop the constraint.
CONS: Seems unnecessary to create an index just to delete it.

Another approach would be to alter the generated migration, so we add it has nullable, update all the values and then make it non-nullable.

public override void Up()
{
    AddColumn("dbo.SomeTable", "NewColumn", c => c.Int());
    Sql("UPDATE dbo.SomeTableSET NewColumn= 1");
    AlterColumn("dbo.SomeTable", "NewColumn", c => c.Int(nullable: false));
}

PROS: Seems simpler/cleaner

CONS: Have to alter my constraints temporarily (I assume this runs in a transaction and thus we shouldn’t be allowing bad data in). The update might be slow on big tables.

Which method is preferable? Or is there a better way that I’m missing?

Note: I have demonstrated the case where you are adding and cleaning column definitions in one go. If you are just cleaning up defaults from previous migrations, the second approach isn’t helpful.

  • 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-04T23:13:41+00:00Added an answer on June 4, 2026 at 11:13 pm

    Personally I can see nothing wrong with the first approach. Yes, you have to create a default constraint to add a non-nullable column to a non-empty dataset. And yes, you have to delete it afterwards if you need to make sure the new column is always added explicitly in the future, as per your requirement.

    And even if you still have issues with this approach, the problem is, there’s most likely no other alternative apart from your second approach. And the cost of updating a possibly large table with a default value would seem to me greater than the cost of creating and immediate dropping a default constraint.

    I might consider a slight modification, though: I might create the constraint in SQL to avoid the fuss of looking up the default name assigned by the engine, something like this:

    Sql("ALTER TABLE tablename
      ADD columnname type NOT NULL
      CONSTRAINT DF_tablename_columnname DEFAULT defaultvalue");
    

    (Could be rewritten to use a helper function.)

    Dropping a constraint would then be as trivial as executing a single ALTER TABLE statement:

    Sql("ALTER TABLE tablename
      DROP CONSTRAINT DF_tablename_columnname");
    

    (Again, a helper function could easily be used here.)

    But that all might be a T-SQL developer in me speaking louder than a C# one. So you might very well go with the code like the example you’ve posted.

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

Sidebar

Related Questions

I want to create multilingual URLs for my ASP.NET MVC 3 project. Non-default language
Is there a way to create a non nullable type in C# (like DateTime
Up to now I have always adding a new non-MFC C++ class in Visual
In X code, If I create a new project with automatic reference counting (ARC),
I have trouble with adding new row to my UITableView. I read similar questions
Grabbing delimited dates from database and adding new ones to the list, then want
I have situation where I need to change the order of the columns/adding new
Clojure structs can be arbitrarily extended, adding new fields. Is it possible to extend
Currently in my controller, when adding new data, I validate the inputs, and if
I have a TreeView control on a form. I am dynamically adding new TreeNodes

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.