DateCreated or Modified Column - Entity Framework or using triggers on SQL Server

后端 未结 4 2219
失恋的感觉
失恋的感觉 2020-12-16 00:52

After I read one question in attached link, I got a sense of how to set DateCreated and DateModified columns in Entity Framework and use it in my application. In the old SQ

4条回答
  •  抹茶落季
    2020-12-16 01:00

    I agree with marc_s - much safer to have the trigger(s) in the database. In my company's databases, I require each field to have a Date_Modified, Date_Created field, and I even have a utility function to automatically create the necessary triggers.

    When using with Entity Framework, I found I needed to use the [DatabaseGenerated] annotation with my POCO classes:

    [Column(TypeName = "datetime2")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime? Date_Modified { get; set; }
    
    [Column(TypeName = "datetime2")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime? Date_Created { get; set; }
    

    I was attempting to use stored procedure mapping on an entity, and EF was creating @Date_Modified, @Date_Created parameters on my insert/update sprocs getting the error

    Procedure or function has too many arguments specified.

    Most of the examples show using [NotMapped], which will allow select/insert to work, but then those fields will not show up when that entity is loaded!

    Alternately you can just make sure any sprocs contain the @Date_Modified, @Date_Created parameters, but this goes against the design of using triggers in the first place.

提交回复
热议问题