EF 4.1 - How to add a default on insertion for datetime column

后端 未结 6 613
野性不改
野性不改 2021-01-20 01:25

Using EF 4.1 how could I add a default value to the underlying table? In this particular case how could I set a datetime column to the equivalent of getdate every time I in

相关标签:
6条回答
  • 2021-01-20 01:59

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

    0 讨论(0)
  • 2021-01-20 02:00

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

    A snippet from the T4 template where the constructor is created, followed by the partial method. Note 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();
    

    That will result in a generated entity 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 讨论(0)
  • 2021-01-20 02:01

    Entity Framework itself has not a mechanism for it. You have to do it manually in the db or the code.

    0 讨论(0)
  • 2021-01-20 02:06

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

    0 讨论(0)
  • 2021-01-20 02:09

    The solution proposed by @elkdanger is way to go but just if you use code-first approach you don't have to create partial class - you can place initialization directly to your entity.

    Don't use database approach! It will not work because it would demand marking property as database generated (to be correctly repopulated after insert). Once you mark property database generated you can never change its value in the application.

    The last option is overriding SaveChanges in your derived DbContext and setting the property manually. Something like:

    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 can be better if there can be significant difference between creating an instance of the entity and saving the instanance.

    0 讨论(0)
  • 2021-01-20 02:16

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

    if you have the default values configured on the database.

    0 讨论(0)
提交回复
热议问题