Entity Framework Code First - EntityContainer 必须是唯一的
Entity Framework Code First - EntityContainer must be unique
我有以下两个模型。每次我尝试添加迁移和更新数据库时,我都会收到以下错误。
Applicant_Addresses: Name: Each member name in an EntityContainer must
be unique. A member with name 'Applicant_Addresses' is already
defined.
我搜索过 SO 并尝试按照 link 中的建议删除 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);
}
}
您可以使用
更改配置的最后一行
modelBuilder.Entity<Applicant_Address>().HasRequired(_ => _.Applicant).WithMany(_ => _.Addresses).HasForeignKey(a => a.Applicant_Id);
我有以下两个模型。每次我尝试添加迁移和更新数据库时,我都会收到以下错误。
Applicant_Addresses: Name: Each member name in an EntityContainer must be unique. A member with name 'Applicant_Addresses' is already defined.
我搜索过 SO 并尝试按照 link 中的建议删除 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);
}
}
您可以使用
更改配置的最后一行modelBuilder.Entity<Applicant_Address>().HasRequired(_ => _.Applicant).WithMany(_ => _.Addresses).HasForeignKey(a => a.Applicant_Id);