C# 实体框架迁移不';t包括默认值数据注释(EF5RC)
我有一个类似这样的类:C# 实体框架迁移不';t包括默认值数据注释(EF5RC),c#,entity-framework-migrations,entity-framework-5,C#,Entity Framework Migrations,Entity Framework 5,我有一个类似这样的类: [Table("Subscribers", Schema = "gligoran")] public class Subscriber { [Key] public string Email { get; set; } [Required] [DefaultValue(true)] public bool Enabled { get; set; } } Enabled = c.Boolean(nullable: false, d
[Table("Subscribers", Schema = "gligoran")]
public class Subscriber
{
[Key]
public string Email { get; set; }
[Required]
[DefaultValue(true)]
public bool Enabled { get; set; }
}
Enabled = c.Boolean(nullable: false, defaultValue: true),
创建包含此类的迁移时,我得到:
public partial class AddSubscriberClass : DbMigration
{
public override void Up()
{
CreateTable(
"gligoran.Subscribers",
c => new
{
Email = c.String(nullable: false, maxLength: 128),
Enabled = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.Email);
}
public override void Down()
{
DropTable("gligoran.Subscribers");
}
}
我希望启用的行如下所示:
[Table("Subscribers", Schema = "gligoran")]
public class Subscriber
{
[Key]
public string Email { get; set; }
[Required]
[DefaultValue(true)]
public bool Enabled { get; set; }
}
Enabled = c.Boolean(nullable: false, defaultValue: true),
当然我可以自己做,但我只是想问是否有办法让实体框架自动完成这项工作
我正在使用最新的Entity Framework 5 RC(5.0.0-RC.net40)。EF根本不使用DefaultValue
属性=它不是模型的一部分,因此迁移不会看到它。您可以在上建议支持此注释。作为Ladislav评论的额外内容。这是正确的。你不能在模型里做这件事。如果您想使用基于代码的迁移。Ie使用PM commandlet添加迁移/更新数据库,然后这种方法将生成的类引入到流程中。然后你可以有默认值。请参阅从DBMigrations派生的类。看见
可以使用特定的列生成器Lamda表达式。这允许默认设置
namespace MigrationsDemo.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class SomeClassThatisATable : DbMigration
{
public override void Up()
{
AddColumn("MyTable", "MyColumn", c => c.String( defaultvalue:"Mydefault" ));
}
我想只有手工操作是唯一的选择。不过,谢谢你的用户语音链接。我有一些想法。实际上今天微软发布了,所以你们甚至可以自己尝试;)美好的前几天我在找它,结果发现它没有打开。我喜欢微软新的开放政策。这是一个现有的政策。