简体   繁体   中英

Entity Framework 6.1: Multiple added entities may have the same primary key

I'm running into an issue with Entity Framework, and this is the error: Unable to determine the principal end of the 'Force.Data.Models.Employee_Office' relationship. Multiple added entities may have the same primary key. Unable to determine the principal end of the 'Force.Data.Models.Employee_Office' relationship. Multiple added entities may have the same primary key. I can't figure out what the issue is and I've been staring at it for three hours now. Here's the code, could someone point me in the right direction because I can't seem to:

Employee.cs

public partial class Employee : Person, IUser<int> {
    public int Id { get; set; }

    #region Relationship Properties
    public byte CompanyId { get; set; }
    public short OfficeId { get; set; }
    public int? ManagerId { get; set; }

    public virtual ICollection<Address> Addresses { get; private set; }
    public virtual Company Company { get; set; }
    public virtual ICollection<Device> Devices { get; private set; }
    public virtual ICollection<Email> Emails { get; private set; }
    public virtual ICollection<Employee> Employees { get; private set; }
    public virtual Employee Manager { get; set; }
    public virtual Office Office { get; set; }
    public virtual ICollection<Phone> Phones { get; private set; }
    public virtual ICollection<Role> Roles { get; private set; }
    #endregion
}

Office.cs

public partial class Office {
    public short Id { get; set; }

    #region Relationship Properties
    public int AddressId { get; set; }
    public short RegionId { get; set; }

    public virtual Address Address { get; set; }
    public virtual ICollection<Employee> Employees { get; private set; }
    public virtual ICollection<Job> Jobs { get; private set; }
    public virtual ICollection<Lead> Leads { get; private set; }
    public virtual ICollection<Phone> Phones { get; private set; }
    public virtual Region Region { get; set; }
    #endregion
}

EmployeeConfiguration.cs

internal sealed class EmployeeConfiguration : EntityTypeConfiguration<Employee> {
    public EmployeeConfiguration() {
        this.ToTable("Employees");

        this.HasKey(
            k =>
                k.Id);

        #region Properties
        #endregion

        #region Relationships
        /// Employee has a 1:* relationship with Offices.
        this.HasRequired(
            t =>
                t.Office).WithMany(
            t =>
                t.Employees).HasForeignKey(
            k =>
                k.OfficeId);
        #endregion
    }
}

OfficeConfiguration.cs

internal sealed class OfficeConfiguration : EntityTypeConfiguration<Office> {
    public OfficeConfiguration() {
        this.ToTable("Offices");

        this.HasKey(
            k =>
                k.Id);

        #region Properties
        this.Property(
            p =>
                p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        #endregion

        #region Relationships
        #endregion
    }
}

Here's also a screenshot of the generated database, which looks fine to me. I don't think it's the database that's yelling at me, but rather EF beinc confused about something...

在此处输入图片说明

So, I'm an idiot, the problem was looking at me this whole time... It turned out it was the Seed method that was failing. In it I was adding 40 Employee objects, but one of them did not have an Office assigned to it and that's why it was failing. Ugh, I need a nap...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM