0

나는 코드 첫번째 스키마를 사용하여 개체를 만드는 오전 있지만 응용 프로그램의 생성 예외 실행할 때Entity Framework - 형식 간의 주된 끝을 결정할 수 있습니까?

Unable to determine the principal end of an association between the types 'WebApplication1.Models.DateOfProject' and 'WebApplication1.Models.Projects'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

내 시나리오는 프로젝트 1 개 프로젝트 1 dateOfProject을 갖도록 DateOfProjects 사이 1.1의 관계를 구현하는 것입니다.

내 코드

public class Projects 
    { 
     [Key()] 
     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 
     public int ProjectId { get; set; } 
     public string ProjectTitle { get; set; } 

     public string ProjectDescriptions { get; set; } 

     public DateOfProject DateOfProject { get; set; } 

     public virtual ICollection<ApplicationUser> ApplicationUser { get; set; } 

     public virtual ICollection<TaskSheetManagement> TaskSheetManagement { get; set; } 
    } 

    public class DateOfProject 
    { 
     public int DateOfProjectId { get; set; } 

     [ForeignKey("ProjectId")] 
     public Projects Projects { get; set; } 

     public DateTime DateOfProjectCreation { get; set; } 

     public Nullable<DateTime> ExpectedCompletionDate { get; set; } 

     public Nullable<DateTime> ProjectCompletionDate { get; set; } 


    } 

하고 DbContextClass inOnModelCreating 기능 내부

modelBuilder.Entity<Projects>().HasKey(pk => pk.ProjectId).ToTable("Projects"); 
modelBuilder.Entity<DateOfProject>().HasKey(pk => pk.DateOfProjectId).ToTable("DateOfProject"); 
modelBuilder.Entity<Projects>().HasRequired(p => p.DateOfProject).WithRequiredPrincipal(c => c.Projects); 

난 그냥 그 문제를 해결할 수 있습니다.

+0

가능한 중복 [연결의 주된 결론을 결정할 수 없음] (http://stackoverflow.com/questions/20035021/unable-to-determine-the-principle-end-of-an-association) – Colin

답변

0

1 : 1 관계가 필요하면 [ForeignKey ("ProjectId")] 특성을 제거해야합니다. 1 : 1 관계의 경우 기본 키가 사용됩니다. 별도의 외부 키 열을 원하면 1 : * 관계입니다.

관련 문제