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困惑的东西...
当与不例外发生在哪里? –
当上下文初始化数据库时,在'SaveChanges'上,我想。 – Gup3rSuR4c
你可能有这个同样的问题,一个http://stackoverflow.com/questions/8530581/multiple-added-entities-may-have-the-same-primary-key-in-entity-framework –