2016-06-03 3 views
3

'Employee'및 'Department'이라는 테이블에 대해 DbContext를 구현하려고합니다. 직원과 부서 간의 관계는 여러 항목으로 이루어집니다. 즉 부서에는 많은 직원이있을 수 있습니다.EntityFramework : 잘못된 열 이름 * _ID1

다음은 EF가 Department_ID1 언급되는 이유는 확실하지 오전 EntityFramework의 내가 디자인 클래스 (CodeFirst 방식)

[Table("Employee")] 
    public class Employee 
    { 
     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 
     public int Id { get; set; } 

     [Column("Name")] 
     public string Name { get; set; } 

     [Column("Department_ID")]   
     public int Department_ID { get; set; } 

     public virtual Department Department { get; set; } 
    } 

[Table("Department")] 
    public class Department 
    { 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int ID { get; set; } 

     [Column("Name")]   
     public string Name { get; set; } 

     public virtual ICollection<Employee> Employees { get; set; } 
    } 

나는 예외

"Invalid column name 'Department_ID1'." 

아래에 무엇입니까 직원 레코드를 추가하는 동안이다. DbContextOnModelCreating 방법으로 구성을 추가해야합니까? 내가 Employee 클래스의 public virtual Department Department { get; set; } 재산 ForeignKey 특성을 사용하여이 문제를 해결할 수있는 몇 시간을 소비 한 후 EF의 버전 6.1.1

답변

6

안녕을 사용하고

.

아래 코드를 참조하십시오.

[Table("Employee")] 
public class Employee 
{ 
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    [Column("Name")] 
    public string Name { get; set; } 

    [Column("Department_ID")]   
    public int Department_ID { get; set; } 

    [ForeignKey("Department_ID")] 
    public virtual Department Department { get; set; } 
} 

내 문제가 해결되었습니다. 이 문제를 해결할 다른 해결책이 있습니까? 유창한 API 사용하기?

0

나는 또한 onemany 재산의 List을 가지고 내 EFone-many 거래에서이 문제를 왔 내 매핑은 그 속성을 지정하지 않았습니다. 의 Exception합니다 (잘못된 방법)을 발생하는 방법 다음

public class Notification 
{ 
    public long ID { get; set; }  

    public IList<NotificationRecipient> Recipients { get; set; } 
} 

내 매핑 그런

public class NotificationRecipient 
{ 
    public long ID { get; set; } 

    public long NotificationID { get; set; } 

    public Notification Notification { get; set; } 
} 

: 그것을 고정 무엇

builder.HasOne(x => x.Notification).WithMany() 
    .HasForeignKey(x => x.NotificationID); 

를 (예를 들어 가지고 올바른 방식)은 WithMany 속성을 지정하고 있습니다 :

builder.HasOne(x => x.Notification).WithMany(x => x.Recipients) 
    .HasForeignKey(x => x.NotificationID);