简体   繁体   English

实体框架6.1:添加的多个实体可能具有相同的主键

[英]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. 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 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 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 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... 我不认为是数据库对我大吼大叫,而是让EF beinc对某些事情感到困惑...

在此处输入图片说明

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. 所以,我是个白痴,问题一直困扰着我……原来是Seed方法失败了。 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. 在其中添加了40个Employee对象,但是其中一个没有分配给Office ,这就是失败的原因。 Ugh, I need a nap... gh,我需要午睡...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个添加的实体可能在实体框架中具有相同的主键 - Multiple added entities may have the same primary key in Entity Framework 对于某些循环实体,多个添加的实体可能具有相同的主键 - multiple added entities may have the same primary key for some cyclic entity 多个添加的实体可能具有相同的主键 - Multiple added entities may have the same primary key EF6:多个添加的实体可能具有相同的主键 - EF6: Multiple added entities may have the same primary key 多个添加的实体可能在数据库种子上具有相同的主键 - multiple added entities may have the same primary key on database seed 无法确定关系的主要终点,多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the relationship, Multiple added entities may have the same primary key 无法确定 X 关系的主端。 多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the X relationship. Multiple added entities may have the same primary key EF 6.多个添加的实体可能具有相同的主键。 错误 - EF 6. Multiple added entities may have the same primary key. Error 无法确定etaxiDataModel关系的主要结尾。 多个添加的实体可以具有相同的主键 - Unable to determine the principal end of the etaxiDataModel relationship. Multiple added entities may have the same primary key 无法确定“ Vehicle_VehicleClass”关系的主要结尾。 多个添加的实体可能具有相同的主键 - Unable to determine the principal end of the 'Vehicle_VehicleClass' relationship. Multiple added entities may have the same primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM