0

I have two models as below. Each time I try to add a migration and update the database I get the following error.

Applicant_Addresses: Name: Each member name in an EntityContainer must be unique. A member with name 'Applicant_Addresses' is already defined.

I've searched SO and tried deleting the DLL's cleaning the project etc as per the suggestions in the link, I have even re-creating a new project with minimal code but all to no avail.

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

Can anyone see what I am missing?

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);
   }
}
Community
  • 1
  • 1
Steve H
  • 63
  • 1
  • 9

1 Answers1

1

You can change the last line of your configuration with

modelBuilder.Entity<Applicant_Address>().HasRequired(_ => _.Applicant).WithMany(_ => _.Addresses).HasForeignKey(a => a.Applicant_Id);
bubi
  • 6,414
  • 3
  • 28
  • 45