2014-02-06 62 views
1

我运行到与实体框架的一个问题,这就是错误:Unable to determine the principal end of the 'Force.Data.Models.Employee_Office' relationship. Multiple added entities may have the same primary key.我想不出是什么问题,我一直在盯着在那里现在三个小时。下面的代码,可能有人点我在正确的方向,因为我似乎不能:实体框架6.1:多添加的实体可能具有相同的主键

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 
    } 
} 

这里也是生成的数据库,这看起来好像没什么问题的屏幕截图。我不认为这是一个的叫喊我的数据库,而是EF beinc困惑的东西...

enter image description here

+0

当与不例外发生在哪里? –

+0

当上下文初始化数据库时,在'SaveChanges'上,我想。 – Gup3rSuR4c

+0

你可能有这个同样的问题,一个http://stackoverflow.com/questions/8530581/multiple-added-entities-may-have-the-same-primary-key-in-entity-framework –

回答

4

所以,我是个白痴,这个问题是在看我这整个时间。 ..原来这是Seed方法失败。在这里面,我加40个Employee对象,但他们中的一个没有分配给它的Office,这就是为什么它是失败的。唉,我需要一个午睡...

相关问题