2016-10-04 36 views
0

重要信息:我發現很多類似的問題都是關於Unable 以確定關係的主要目的。在SO中,多個添加的 實體可能具有相同的主鍵異常。我閱讀並理解 是什麼意思,它爲什麼會發生。實體框架 - 無法確定主體端點

但是,我無法找到問題在我的模型隱藏特別。如果你能指出究竟做錯了什麼,我將非常感激。 模型在這裏:

public class Faculty : Entity 
{ 
    public string Name { get; set; } 
    public string Acronym { get; set; } 
    public string Description { get; set; } 

    public ICollection<Department> Departments { get; set; } 
    public ICollection<Specialty> Specialties { get; set; } 
    public ICollection<Student> Students { get; set; } 
    public ICollection<Employee> Employees { get; set; } 
} 

public class Department : Entity 
{ 
    public string Name { get; set; } 
    public string Acronym { get; set; } 
    public string Description { get; set; } 
    public Guid ? FacultyId { get; set; } 
    public Faculty Faculty { get; set; } 

    public ICollection<Subject> Subjects { get; set; } 
    public ICollection<Employee> Employees { get; set; } 
    public ICollection<Specialty> Specialties { get; set; } 
} 

public class Specialty : Entity 
{ 
    public string Name { get; set; } 
    public string Acronym { get; set; } 
    public string Description { get; set; } 
    public ICollection<Student> Students { get; set; } 
    public Guid FacultyId { get; set; } 
    public Faculty Faculty { get; set; } 
    public Guid DepartmentId { get; set; } 
    public Department Department { get; set; } 
} 

配置:映射在這裏

public class DepartmentConfiguration : EntityConfiguration<Department> 
{ 
    public DepartmentConfiguration() 
    { 
     Property(p => p.Name).IsRequired().HasMaxLength(200); 
     Property(p => p.Acronym).IsRequired().HasMaxLength(5); 
     Property(p => p.Description).HasMaxLength(1000); 

     HasMany(p => p.Subjects).WithRequired(p => p.Department); 
     HasMany(p => p.Employees).WithOptional(p => p.Department); 
     HasMany(p => p.Specialties).WithRequired(p => p.Department); 
    } 
} 

public class SpecialtyConfiguration : EntityConfiguration<Specialty> 
{ 
    public SpecialtyConfiguration() 
    { 
     Property(p => p.Name).IsRequired().HasMaxLength(200); 
     Property(p => p.Acronym).IsRequired().HasMaxLength(5); 
     Property(p => p.Description).HasMaxLength(1000); 
     HasMany(p => p.Students).WithRequired(p => p.Specialty); 
    } 
} 

public class FacultyConfiguration : EntityConfiguration<Faculty> 
{ 
    public FacultyConfiguration() : base() 
    { 
     Property(p => p.Name).IsRequired().HasMaxLength(200); 
     Property(p => p.Acronym).IsRequired().HasMaxLength(5); 
     Property(p => p.Description).HasMaxLength(1000); 

     HasMany(p => p.Departments).WithOptional(p => p.Faculty); 
     HasMany(p => p.Specialties).WithRequired(p => p.Faculty); 
     HasMany(p => p.Employees).WithOptional(p => p.Faculty); 
     HasMany(p => p.Students).WithRequired(p => p.Faculty); 
    } 
} 

和異常說,大約部門和專業之間的關係。

無法確定'EMIS.DAL.Context.Department_Specialties'關係的主體末端。多個添加的 實體可能具有相同的主鍵。

回答

0

解決了這個問題。映射中沒有錯誤。我忘了,通過使用DatabaseGeneratedOption.Identity PK僅在將數據保存到數據庫時才添加。但是,我試圖將ID爲null的專業添加到Department,直到它不存在於DB中。