2014-02-06 63 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,這就是爲什麼它是失敗的。唉,我需要一個午睡...

相關問題