告诉EF自动计算并插入创建行的当前日期时间? [英] Tell EF to automatically compute and insert the current date time when the row is created?
问题描述
我正在使用EF 6,MVC 5的Code First方法
搜索后,我得到了这个
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreatedDate {get;组;
它必须以模型类编写,但是当插入新记录时,NULL被保存在DB中,我注意到这只是CreatedDate的声明,在哪里编写CreatedDate = DateTime.Now
或任何其他方式来解决它。
我的整个模型
namespace December.Models
{
[Table(tblLibray)]
public class Library
{
[Key]
public int Id {get;组; }
[必需]
public string BookName {get;组; }
[必需]
public string Author {get;组; }
[必需]
public string说明{get;组; }
[必需]
公开十进制MRP {get;组; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreatedDate {get;组;
}
}
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
属性允许您跳过实体框架验证,但由于您的 DateTime
可空
,您不需要它。
创建一个新课程
内部类AutoDateTimeMigrationSqlGenerator:SqlServerMigrationSqlGenerator //:NpgsqlMigrationSqlGenerator
{
protected override void Generate(AddColumnOperation addColumnOperation)
{
SetCreatedDateColumn(addColumnOperation.Column);
base.Generate(addColumnOperation);
}
protected override void生成(CreateTableOperation createTableOperation)
{
SetCreatedDateColumn(createTableOperation.Columns);
base.Generate(createTableOperation);
}
private static void SetCreatedDateColumn(IEnumerable< ColumnModel>列)
{
foreach(列中的var columnModel)
{
SetCreatedDateColumn (columnModel);
}
}
private static void SetCreatedDateColumn(PropertyModel column)
{
if(column.Name ==CreatedDate)
{
column.DefaultValueSql =GETUTCDATE();
}
}
}
在您的 Configuration.cs
文件(具有从 DbMigrationsConfiguration
继承的迁移配置类的文件)在类构造函数中添加以下行:
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator(System.Data.SqlClient,new AutoDateTimeMigrationSqlGenerator())
}
PS:更新数据库
所有这些。
I want to insert current Datetime when ever a new row is inserted.
I am using Code First Approach of EF 6,MVC 5
After searching I got this
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreatedDate { get; set; }
It has to be written in Models Class,but when ever a new record is inserted NULL is saved in DB,I have noticed that this is just declaration of CreatedDate,where to write CreatedDate = DateTime.Now
OR any other way to solve it.
My Entire Model
namespace December.Models
{
[Table("tblLibray")]
public class Library
{
[Key]
public int Id { get; set; }
[Required]
public string BookName { get; set; }
[Required]
public string Author { get; set; }
[Required]
public string Description { get; set; }
[Required]
public decimal MRP { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime? CreatedDate { get; set; }
}
}
The [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
attribute allows you to skip entity framework validation, but since your DateTime
prop is nullable
, you don't need it.
Create a new class
internal class AutoDateTimeMigrationSqlGenerator: SqlServerMigrationSqlGenerator //:NpgsqlMigrationSqlGenerator
{
protected override void Generate(AddColumnOperation addColumnOperation)
{
SetCreatedDateColumn(addColumnOperation.Column);
base.Generate(addColumnOperation);
}
protected override void Generate(CreateTableOperation createTableOperation)
{
SetCreatedDateColumn(createTableOperation.Columns);
base.Generate(createTableOperation);
}
private static void SetCreatedDateColumn(IEnumerable<ColumnModel> columns)
{
foreach (var columnModel in columns)
{
SetCreatedDateColumn(columnModel);
}
}
private static void SetCreatedDateColumn(PropertyModel column)
{
if (column.Name == "CreatedDate")
{
column.DefaultValueSql = "GETUTCDATE()";
}
}
}
In your Configuration.cs
file (the one with the migration configuration class that inherits from DbMigrationsConfiguration<>
) add the following line in the class constructor:
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("System.Data.SqlClient", new AutoDateTimeMigrationSqlGenerator())
}
PS: Update-Database
after all this.
这篇关于告诉EF自动计算并插入创建行的当前日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!