Entity Framework and Default Date

entity-framework

Question

I have a SQL table that contains a DateTime column with the value GetDate by default ()

Instead of using the local date and time of the computer the console app is running on, I would like entity framework to utilize the SQL date and time (the SQL server is 1 hour behind).

Additionally, since the column does not permit null values, when I enter the date 1/1/0001, I receive the following error:

An out-of-range value was produced by converting a datetime2 data type to a datetime data type. The statement has come to an end.

I'm grateful.

1
5
2/6/2013 12:17:19 PM

Accepted Answer

  1. Launch the edmx designer.
  2. Choose the DateTime column.
  3. Visit properties to make changes.StoreGeneratedPattern from None to Computed

By telling EF not to insert a value for that column, the database will generate the field's default value. Remember that passing some values won't be possible.

21
2/12/2013 4:06:25 PM

Popular Answer

Simply add the following to your DbContext class if you're utilizing the Fluent API:

        modelBuilder.Entity<EntityName>()
            .Property(p => p.PropertyName)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow