【发布时间】:2011-03-09 10:34:32
【问题描述】:
在 Entity Framework Code First 方法中,如何为 POCO 的 EntityConfiguration 类中的属性设置默认值?
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
}
public class PersonConfiguration : EntityConfiguration<Person>
{
public PersonConfiguration()
{
Property(p => p.Id).IsIdentity();
Property(p => p.Name).HasMaxLength(100);
//set default value for CreatedOn ?
}
}
【问题讨论】:
-
万一有人想知道。我最终使用“存储库模式”(stackoverflow.com/questions/3175/…),在插入期间设置默认值。这是一个很好的分离,因为我正在考虑在未来离开 EF。
-
+1 表示“将来远离 EF”。浪费了很多时间,我本来可以为我当前的应用程序编写 SQL 代码!但是你打算用什么? NHibernate,是不是更好?
标签: .net entity-framework ef-code-first entity-framework-4