0
我想將CreatedDate和ModifiedDate列添加到現有數據庫。我需要的是每當一行被添加到表格中時,CreatedDate
和ModifiedDate
將使用GETDATE()
自動設置其值,它們之間的差值是ModifiedDate
將在每次修改行時繼續更新。我一直在環顧四周,發現混合答案。這裏是我的代碼:EF6代碼首先添加CreatedDate和ModifiedDate列到現有數據庫
實體
public class Student
{
public int StudentID {get; set;}
public string Name {get; set;}
//I want to add CreatedDate and ModifiedDate here
}
語境:
public class TestContext: DbContext
{
public TestContext()
: base("TestContext")
{
}
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Student
modelBuilder.Entity<Student>().HasKey(s => s.StudentID);
modelBuilder.Entity<Student>().Property(s => s.Name).IsRequired();
}
}
任何幫助將不勝感激。
我想你只回答OP的一半問題。 –