实体框架代码首先 - 不能添加新类

时间:2012-02-02 19:38:21

标签: entity-framework-4.1 ef-code-first

我首先使用代码,然后设置了我的环境,一切都很顺利。问题是我稍后回来并需要添加一个新类(Foo),例如。

public class NorthwindContext : DbContext 
{
    public DbSet<Customer> Customers { get; set; }
    ...
    public DbSet<Foo> Foos { get; set; } // added
}

public class Foo // added
{
    public string FooID { get; set; }
    public int Payload { get; set; }
}

public class Customer
{
...

但是,现在每当我尝试更新服务引用时,都会收到错误消息。如果我拿出

public DbSet<Foo> Foos { get; set; }

一切顺利,但生成的代码中没有Foos。我错过了什么?

1 个答案:

答案 0 :(得分:0)

您没有多少选择来解决此问题。删除上下文中包含的IncludeMetadataConvention。然后手动删除数据库。

public class NorthwindContext : DbContext
{
    public DbSet<Customer> Customers { get; set; }

    public DbSet<Foo> Foos { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

        base.OnModelCreating(modelBuilder);
    }
}

或在程序入口点设置初始化程序。

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<NorthwindContext>());

最终选项是使用仍处于测试阶段的Code First Migrations

相关问题