2014-10-21 12 views
2

데이터베이스에 두 개의 테이블이 있습니다. 하나는 [부모]이고 다른 하나는 [자식]입니다 - 외래 키로 ParentId가 있습니다.Entity Framework에서 하위 레코드를 추가 할 때 부모를 만드는 것을 피하는 방법은 무엇입니까?

그래서 부모와 자녀 사이의 관계는 1 대다수입니다.

문제는 새로운 아동용 레코드를 만들려고 할 때 새로운 부모를 만드는 것이 었습니다.

어떻게 해결할 수 있습니까? 당신이 Add()를 호출하기 전에 당신이

enter image description here 지금까지 게시 한 내용을 바탕으로

public void Add(Model.Pages.PageGroup entity) 
     { 
      using (var ctx = new SmartDbContext()) 
      { 
       var dbEntity = entity.ToEntity(); 
       ctx.PageGroups.Add(dbEntity); 
       ctx.SaveChanges(); 
      } 
     } 

    namespace SmartECM.Repository.EF 
    { 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.ComponentModel.DataAnnotations.Schema; 
    using System.Data.Entity.Spatial; 

    public partial class PageGroup 
    { 
     public PageGroup() 
     { 
      Pages = new HashSet<Page>(); 
     } 

     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public Guid GroupId { get; set; } 

     [Required] 
     [StringLength(100)] 
     public string GroupName { get; set; } 

     public Guid ProjectId { get; set; } 

     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)] 
     public DateTime? DateAdded { get; set; } 
     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)] 
     public DateTime? DateUpdated { get; set; } 

     public virtual Project Project { get; set; } 

     public virtual ICollection<Page> Pages { get; set; } 
    } 
} 


    namespace SmartECM.Repository.EF 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.ComponentModel.DataAnnotations.Schema; 
    using System.Data.Entity.Spatial; 

    public partial class Project 
    { 
     public Project() 
     { 
      PageGroups = new HashSet<PageGroup>(); 
     } 

     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public Guid ProjectId { get; set; } 

     [Required] 
     [StringLength(100)] 
     public string ProjectName { get; set; } 

     public string ProjectDescription { get; set; } 

     public Guid? Owner { get; set; } 

     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)] 
     public DateTime? DateAdded { get; set; } 

     [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Computed)] 
     public DateTime? DateUpdated { get; set; } 

     public virtual ICollection<PageGroup> PageGroups { get; set; } 
    } 
} 
+0

해당 엔티티 필드를 채우는 코드를 표시 할 수 있습니까? 내가 뭘 잘못했는지는 알 것 같지만, 그 코드를 볼 때까지 나는 확실히 알지 못할 것이다. – IronMan84

+0

@ IronMan84 예, 선생님. 업데이트 됨. 지금 그것을보십시오. – shanke

+0

페이지 클래스는 어디에 있습니까? –

답변

2

, 당신의 문제는 당신이 즉, 질문 (의 실체에 값을 설정하는 곳에서오고 있다고 생각 감사 방법). 내가 생각한 바에는 PageGroup 엔티티의 Project 탐색 속성을 설정하고 그런 식으로 저장하려고한다는 것입니다.

그런 식으로 당신이 ProjectId 필드가 null 인 것을 EF가 보는 컬렉션에 PageGroup 개체를 추가 할 때 Project 탐색 속성에있는 데이터가 현재 사용하지 않는 새로운 프로젝트라고 가정이다 그 일에 대한 문제 시스템에서. 그런 다음 데이터베이스에 해당 "새"프로젝트를 만들어 해당 컬렉션에 PageGroup 엔티티를 추가합니다.

그러면 솔루션은 연결된 필드의 ID를 탐색 속성에 할당하지 않고 프로젝트의 ID로 채 웁니다. 그렇게하면 EF는 어느 프로젝트에 엔티티를 할당하려고하는지 알 수 있습니다.