Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First CTP4 Default Column Values?

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!

like image 326
Lukasz Avatar asked Oct 27 '10 19:10

Lukasz


People also ask

How do I set the default value in Entity Framework first?

Just open your model, right click on the column you want to set a default for, choose properties, and you will see a "DefaultValue" field. Just fill that out and save. It will set up the code for you.

How do I set default value in EDMX?

edmx viewer select an entity -> go the column -> properties -> Default Value. Add the default value for the column in it and that's it.


1 Answers

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;
    }
}
like image 182
Korayem Avatar answered Oct 15 '22 11:10

Korayem