在实体框架代码优先方法中,如何为 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 ?
}
}
随着 Entity Framework 4.3 的发布,您可以通过迁移来完成此操作。
EF 4.3 Code First 迁移演练 http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
所以使用你的例子它会是这样的:
public partial class AddPersonClass : DbMigration
{
public override void Up()
{
CreateTable(
"People",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 100),
CreatedOn = c.DateTime(nullable: false, defaultValue: DateTime.UtcNow)
})
.PrimaryKey(t => t.Id);
}
public overide void Down()
{
// Commands for when Migration is brought down
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)