将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

Entity Framework Migrations don't include DefaultValue data annotation (EF5RC)
Entity Framework 迁移不包括默认值数据注释( EF5RC )

I have a class that looks like this :


[Table("Subscribers", Schema ="gligoran")]
public class Subscriber
{
 [Key]
 public string Email { get; set; }

 [Required]
 [DefaultValue(true)]
 public bool Enabled { get; set; }
}

When creating a migration to include this class i get :


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");
 }
}

I'd like the Enabled line to look like this :


Enabled = c.Boolean(nullable: false, defaultValue: true),

Of course i can do this myself, but i'm just asking if there's a way to make Entity Framework do it automatically.

I'm using the latest Entity Framework 5 RC (5.0.0-rc.net40).

时间: 作者:

EF doesn't use DefaultValue attribute at all = it is not part of the model so migrations don't see it.you can propose support of this annotation on Data UserVoice.

原作者:

As an Extra to Ladislav's Comment.which is correct. you cant do it in the model.if you wanted to use code based migrations.Ie using PM commandlet Add-Migration/Update Database, then this approach introduces a generated class into the process.then you can have defaults.see the classes that Derive from DBMigrations.see http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigration.addcolumn%28v=vs.103%29.aspx you can specific Column builder Lamda expressions.this allows defaults.


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" ));
 }

原作者:
...