2017-01-13 4 views
0

나는 다음과 같은 클래스가O.R.M. 외래 키 제약 조건을 제외하고 충돌 엔티티 프레임 워크 INSERT 문

public class Employee 
{ 
    public Guid Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Gender { get; set; } 
    public int Salary { get; set; } 
    public string PhoneNumber { get; set; } 
    public Guid DepartmentId { get; set; } 
    public virtual Department Department { get; set; } 
    public int Deleted { get; set; } 

    public bool IsDeleted() 
    { 
     return this.Deleted == 1 ? true : false; 
    } 

    public void MarkAsDeleted() 
    { 
     this.Deleted = 1; 
    } 

    public string GetFullName() 
    { 
     return this.FirstName + " " + this.LastName; 
    } 
} 

public class Department 
{ 
    public Department() 
    { 
     this.Employees = new List<Employee>(); 
    } 

    public Guid Id { get; set; } 
    public string Name { get; set; } 
    public virtual List<Employee> Employees { get; set; } 
    public int Deleted { get; set; } 

    public bool IsDeleted() 
    { 
     return this.Deleted == 1 ? true : false; 
    } 

    public void MarkAsDeleted() 
    { 
     this.Deleted = 1; 
    } 

    public void AddEmployee(Employee employee) 
    { 
     this.Employees.Add(employee); 
    } 

    public void RemoveEmployee(Employee employee) 
    { 
     this.Employees.Remove(employee); 
    } 
} 

내가 일대 다 관계를 만들려면 내 구성에서 나는이

에 대한 다음과 같은 코드가 있습니다
[modelBuilder.Entity<Department>() 
      .HasMany(l => l.Employees). 
       WithRequired(r => r.Department). 
       HasForeignKey(r => r.DepartmentId); 

그러나 예외에게 FOREIGN KEY 제약 \ FK_dbo.Employees_dbo.Depa와 충돌

INSERT 문을 발생 rtments_DepartmentId. 데이터베이스 \ Entities \, 테이블 \ dbo.Departments \, 열 ID에서 충돌이 발생했습니다

아무도 도와 줄 수 있습니까? 감사합니다

답변

3

어떤 시점에서
Department없이 저장하려고하는 것 같습니다. 따라서 관계가 실패하고 있습니다.