实体框架代码优先 - EntityContainer必须是唯一的

时间:2016-09-04 02:58:21

标签: entity-framework entity-framework-6 ef-migrations

我有两个型号如下。每次我尝试添加迁移并更新数据库时,都会出现以下错误。

  

Applicant_Addresses:名称:EntityContainer中的每个成员名称必须   独一无二。名为“Applicant_Addresses”的成员已经是   定义

我搜索了SO并尝试删除DLL清理项目等按照链接中的建议,我甚至用最少的代码重新创建了一个新项目,但都无济于事。

The EntityContainer name must be unique. An EntityContainer with the name 'Entities' is already defined

有人能看到我错过的东西吗?

namespace Demo.Data.EntityModels
{
public class Applicant
{
    public Guid Id { get; set; }

    public EnumHelper.Salutation Title { get; set; }

    public string E_FirstName { get; set; }

    public string E_LastName { get; set; }

    public DateTime DateOfBirth { get; set; }

    public virtual IList<Applicant_Address> Addresses { get; set; }
}
}

namespace Demo.Data.EntityModels
{
public class Applicant_Address
{
    public Guid Id { get; set; }

    public Guid Applicant_Id { get; set; }

    public virtual Applicant Applicant { get; set; }

    public EnumHelper.ResidentialStatus ResidentialStatus { get; set; }

    public string E_Flat_Unit { get; set; }

    public string E_Building { get; set; }

    public string E_Street { get; set; }

    public string E_Locality { get; set; }

    public string E_Town { get; set; }

    public string E_County { get; set; }

    public string E_PostCode { get; set; }
}
}

public class DemoContext : DbContext
{
    public DemoContext() : base("DemoContext")
    {
    }

    public DbSet<Applicant> Applicants { get; set; }

    public DbSet<Applicant_Address> Applicant_Addresses { get; set; }


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

        modelBuilder.Entity<Applicant>().HasKey(k => k.Id);
        modelBuilder.Entity<Applicant_Address>().HasKey(k => k.Id);

        modelBuilder.Entity<Applicant>().HasMany(a => a.Addresses).WithRequired(ad => ad.Applicant).HasForeignKey(a => a.Applicant_Id);
   }
}

1 个答案:

答案 0 :(得分:1)

您可以使用

更改配置的最后一行
modelBuilder.Entity<Applicant_Address>().HasRequired(_ => _.Applicant).WithMany(_ => _.Addresses).HasForeignKey(a => a.Applicant_Id);