How can set a default value constraint with Entity Framework 6 Code First?

72,936

Solution 1

Now the answer is Yes:

AddColumn("[table name]", 
          "[column name]", 
          c => c.Boolean(nullable: false, defaultValue: false));

Solution 2

Unfortunately the answer right now is 'No'.

But you can vote for Better support for default values

EDIT 30 Mar 2015: It's coming in EF7... Support database default values in Code First

EDIT 30 Jan 2017: General support for default database values is part of EF Core (the new name for EF7)... Default values

EDIT 17 Jan 2018: I'm not sure why people are commenting that EF7 is still a "pipe dream". EF7 (renamed EF Core) was released on 27th June 2016 and supports the setting of default values in Code First using the FluentAPI like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Blog>()
        .Property(b => b.Rating)
        .HasDefaultValue(3);
}

EF Core 2.0 was released on 14 August 2017

Solution 3

You can set the default value within the constructor

class Foo : Model
{
    public bool DefaultBool { get; set; }

    public Foo()
    {
        DefaultBool = true;
    }
}

When you create a new Foo it will set the true value for the property DefaultBool. This is not a default constraint but it works.

Share:
72,936
CB-Dan
Author by

CB-Dan

I work at ConstructBuy.com as the Vice President R&amp;D.

Updated on July 10, 2022

Comments

  • CB-Dan
    CB-Dan almost 2 years

    In a legacy app, most string properties can't be null and need to have a default value of string.empty.

    I know it's possible to do this with migrations, but I'm looking for a way to do this using the fluent configuration interface:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Properties<string>().Configure(c =>
            {
                c.HasMaxLength(255);
    
                if (!c.ClrPropertyInfo.IsDefined(typeof (NullableAttribute), false))
                {
                    c.IsRequired();
                    // I want to set a default value (string.empty) here.
                }
        }
    

    Is there any way to do this or I'm doomed to initialize all strings in the entity constructors?

  • CB-Dan
    CB-Dan about 9 years
    Thanks. We ended up switching to NHibernate instead, but hopefully this answer will help other people.
  • Jonathan Stark
    Jonathan Stark about 9 years
    How come the DbMigration.AddColumn method is marked as the answer when the question says "I know it's possible to do this with migrations, but I'm looking for a way to do this using the fluent configuration interface" ?
  • Stephen Holt
    Stephen Holt over 8 years
    Agreed. This answer does not answer the question.
  • Dave
    Dave about 8 years
    EF7 is a pipe dream and not usable for production. We need this in EF6 now!
  • async
    async over 7 years
    This doesn't answer the question at all. OP and others (myself included) came here to find out how to set the default constraint on the table.
  • Ariel Moraes
    Ariel Moraes over 7 years
    Only EF7 will support default constraint, that why I answered using this pattern. This is a workaround when using prior EF versions. By the way, I said "this is not a default constraint".
  • Gh61
    Gh61 over 7 years
    I agree, it should not be selected as answer. But maybe it can help other people. I don't know if answer could be unselected. Better answer for now is @Colin's
  • Jonathan Stark
    Jonathan Stark about 6 years
    @Sabre why is it not usable? What problem are you having?
  • Sabre
    Sabre about 6 years
    @Colin The EF Core team themselves acknowledge that there are quite a few features missing before it can replace EF6 as the "recommended version": github.com/aspnet/EntityFrameworkCore/wiki/roadmap
  • user1040323
    user1040323 over 5 years
    I'm astounded. EF has been around for 10 years. The most basic thing I do when designing a database is set default values. This feature should've been present from version 1, 10 years ago. Instead the EF team spend all their time trying to make EF an all singing all dancing answer to the riddle of the universe; and, of course, fail at it. No fit for purpose.
  • enorl76
    enorl76 about 5 years
    @user1040323 agreed. Default values missing from EF(not-core) is kinda silly. EF core is still missing significant features, oh like SqlQuery<T> which itself is kinda silly. So we're stuck between EF and EF core. Sigh.
  • Post Impatica
    Post Impatica almost 5 years
    is there any way to chain these for when you have multiple properties of the same entity that needs to be set? Seems goofy to create a bunch of these for the same entity.
  • user1531040
    user1531040 about 3 years
    And how can you give the Default Constraint the name "DF_Blog_Rating"? modelBuilder.Entityy<Blog>() .Property(b => b.Rating) .HasDefaultValue(3).HasConstraintName("DF_Blog_Rating"); doesn't work.