12

I have been looking into Code First with Entity Framework CTP4 and you can use the ModelBuilder to build up your table columns. Is there a way to set the default value for a column in the database using the ModelBuilder or some other mechanism?

Thank You!

Lukasz
  • 8,710
  • 12
  • 44
  • 72
  • [Relevant suggestion](https://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/2929682-support-database-default-values-in-code-first). – tne Nov 05 '13 at 18:09

4 Answers4

39

I am using the constructor to set the default values. Never failed me

public class Activity
{
    [Required]
    public DateTime AddedDate { get; set; }

    public Activity()
    {
        AddedDate = DateTime.Now;
    }
}
Korayem
  • 12,108
  • 5
  • 69
  • 56
  • This answer is not getting enough love. – Kjensen Mar 01 '11 at 19:27
  • 1
    Only problem is DB default values are more efficient. – Sleeper Smith Mar 15 '11 at 23:37
  • ofcourse, but until it's released in the next release of EF, that will do the trick – Korayem Mar 18 '11 at 15:08
  • 4
    Except that it doesn't actually answer the question, it does provides a work-round but that's not what was asked for and therefore the *right* answer (to the question, as asked) is that its not supported and hence the other is accepted. – Murph Aug 28 '11 at 14:44
  • Because there is no other way to solve it but this way. It's got 17 points, oh wait, it doesnt solve the bug in Entity Framework (which is out of the scope), so it can't be accepted. Bummer. – Korayem Aug 29 '11 at 04:54
  • 2
    Erm, I'll repeat, you're NOT solving the problem and not answering the question - its a work-round and, yes, a valid approach and definitely useful (which is what the upvotes acknowledge) but that still doesn't make it the "right" answer. The upvotes will allow other users to make an assessment of whether it solves the problem for them (although it does't for my use case). – Murph Aug 29 '11 at 10:23
  • Ok, should remove the solution, I mean, the "work around" until it is solved by the real solvers, Microsoft? – Korayem Aug 30 '11 at 06:26
  • 1
    @Murph aparently 21 people believe this is the correct answer. I am thinking of opening a new question, posting my answer there and removing this answer from here. – Korayem Dec 13 '11 at 13:53
  • @Korayem no, they don't - they think that the answer is useful (which it is) but "useful" does not necessarily mean the same thing as "right" in the context of answering a question. Is this being pedantic? Of course it is (-: – Murph Dec 13 '11 at 14:16
  • And none of this works when you are migrating a column from "allow nulls" to "not allow nulls", in which case you need to have a default constraint on the column in your database. – Peter Lillevold Feb 10 '12 at 08:39
  • 1
    This works perfectly with EF6 and a completely blank (starting from scratch) database. – FoggyDay Nov 18 '19 at 21:18
4

In the code generated for a migration, you can specify a default value for a column:

AddColumn("users", "ReceiveSummaryEmail", c => c.Boolean(nullable: false, defaultValue: true));
ciscoheat
  • 3,719
  • 1
  • 35
  • 52
2

This is my way, setting important properties with private setters in a creation method instead via the constructor

Model

public class User
{
    public static User Create(Action<User> init)
    {
        var user = new User();
        user.Guid = Guid.NewGuid();
        user.Since = DateTime.Now;
        init(user);
        return user;
    }

    public int UserID { get; set; }

    public virtual ICollection<Role> Roles { get; set; }
    public virtual ICollection<Widget> Widgets { get; set; }

    [StringLength(50), Required]
    public string Name { get; set; }
    [EmailAddress, Required]
    public string Email { get; set; }
    [StringLength(255), Required]
    public string Password { get; set; }
    [StringLength(16), Required]
    public string Salt { get; set; }

    public DateTime Since { get; private set; }
    public Guid Guid { get; private set; }
}

Creation of new user

context.Users.Add(User.Create(c=>
{
    c.Name = "User";
    c.Email = "some@one.com";
    c.Salt = salt;
    c.Password = "mypass";
    c.Roles = new List<Role> { adminRole, userRole };
}));
321X
  • 3,153
  • 2
  • 30
  • 42
2

Couldn't find a way to add default value other than manualy edit via text editor / application This is a bug in the Entity Framework...

Tor
  • 595
  • 2
  • 10