gli*_*ran 12 c# ef-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; }
}
Run Code Online (Sandbox Code Playgroud)
创建迁移以包含此类时,我得到:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这Enabled条线看起来像这样:
Enabled = c.Boolean(nullable: false, defaultValue: true),
Run Code Online (Sandbox Code Playgroud)
当然我可以自己做,但我只想问是否有办法让Entity Framework自动完成.
我正在使用最新的Entity Framework 5 RC(5.0.0-rc.net40).
Lad*_*nka 11
EF根本不使用DefaultValue属性=它不是模型的一部分,因此迁移不会看到它.您可以在Data UserVoice上建议支持此注释.
作为Ladislav评论的额外内容.哪个是对的.你不能在模型中做到这一点.如果您想使用基于代码的迁移.即使用PM命令行开关Add-Migration/Update Database,则此方法将生成的类引入流程.然后你可以有默认值.请参阅从DBMigrations派生的类.请参阅http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigration.addcolumn%28v=vs.103%29.aspx 您可以使用特定的Column builder 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" ));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10644 次 |
| 最近记录: |