I would like to have a column CreatedDatein my table Accountsdefined as follows:
CreatedDate DATETIME NOT NULL DEFAULT GETUTCDATE()
This will map to the CreatedDate property in the class:
public class Account
{
public DateTime? CreatedDate { get; private set; }
}
When creating a new account, I would like to leave the CreateDate value NULL, and let the database populate it. When loading an existing account from the database, I would like EF to retrieve the CreateDate file created from the database.
I am using the first EF 4.3 code with explicit migration. When EF created the migration for the account table, I changed it as follows:
CreatedDate = c.DateTime(nullable: false, defaultValueSql: "GETUTCDATE()"),
Unfortunately, when I try to map a table using EF 4.3, EF tries to insert a NULL value in the CreatedDate column.
DatabaseGeneratedOption.Identity . . DatabaseGeneratedOption.Computed, NULL. - .
OnModelCreating:
modelBuilder.Entity<Account>().Property(a => a.CreatedDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
- ?
,