2016-10-04 2 views
0

重要:については、多くの類似の質問が見つかりました。 は、関係の主要な終わりを決定します。複数の追加された エンティティが同じプライマリキー例外を持つ可能性があります。私はを読んで、それが何であるのかを理解しているのはなぜですか。問題は私のモデルで特にを隠されている場所エンティティフレームワーク - プリンシパルエンドを特定できません。

しかし、私は見つけることができません。正確に何が間違っているのかを指摘できれば、とても感謝しています。ここ モデル:

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を使って、データベースにデータを保存する瞬間にしかPKが追加されないことを忘れていました。しかし、私はDBに存在しなくなるまでIDがnullに等しいDepartmentにSpecialtyを追加しようとしていました。

関連する問題