EF 4.1 - How to add default insert value for datetime column

Using EF 4.1, how can I add a default value to the base table? In this particular case, how can I set the datetime column to the getdate equivalent every time I insert a new record into the database without setting it to the code.

Thank you in advance

+4
source share
6 answers

The solution offered by @elkdanger is the way to go, but if you use a code-based approach, you don’t need to create a partial class β€” you can place the initialization directly on your entity.

Do not use the database approach! It will not work because it will require the marking property as the generated database (for proper repopulation after insertion). After you mark a property database, you can never change its value in the application.

The last parameter overrides SaveChanges in the derived DbContext and sets the property manually. Sort of:

 public override int SaveChanges() { var entities = ChangeTracker.Entries<YourEntityType>() .Where(e => e.State == EntityState.Added) .Select(e => e.Entity); var currentDate = DateTime.Now; foreach(var entity in entities) { entity.Date = currentDate; } return base.SaveChanges(); } 

This approach may be better if there can be a significant difference between instantiating an object and maintaining instanance.

+7
source

You can create a partial class for your entity, and inside the constructor set the DateTime.Now date column. Thus, every time you create an instance of your class, this field will be set to the current date "automatically".

+2
source

You could (and perhaps should) do this in the table itself, using a trigger or a default value.

0
source

The Entity Framework itself does not have a mechanism. You have to do it manually in db or in code.

0
source

You can also modify your T4 template (.tt file) to add a partial method called from the generated constructor. Then you can create your own partial class and implement the partial method and set the default value.

An excerpt from the T4 template, where the constructor is created, and then the partial method. Pay attention to the last three lines:

 public <#=code.Escape(entity)#>() { <# foreach (var edmProperty in propertiesWithDefaultValues) { #> this.<#=code.Escape(edmProperty)#> = =code.CreateLiteral(edmProperty.DefaultValue)#>; <# } foreach (var navigationProperty in collectionNavigationProperties) { #> this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=code.Escape(navigationProperty.ToEndMember.GetEntityType())#>>(); <# } foreach (var complexProperty in complexProperties) { #> this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>(); <# } #> SetDefaultValues(); } partial void SetDefaultValues(); 

This will result in a generated object having something like:

 public Foo() { // Properties set based on defaults in edmx SetDefaultValues(); } partial void SetDefaultValues(); 

Then in your partial class, you can simply add something like:

 partial void SetDefaultValues() { this.SomeDate = DateTime.Today; } 
0
source

Use [DatabaseGenerated (DatabaseGeneratedOption.Computed)] from System.ComponentModel.DataAnnotations.Schema;

if the database is set to default values.

0
source

All Articles