데이터베이스에 두 개의 테이블이 있습니다. 하나는 [부모]이고 다른 하나는 [자식]입니다 - 외래 키로 ParentId가 있습니다.Entity Framework에서 하위 레코드를 추가 할 때 부모를 만드는 것을 피하는 방법은 무엇입니까?
그래서 부모와 자녀 사이의 관계는 1 대다수입니다.
문제는 새로운 아동용 레코드를 만들려고 할 때 새로운 부모를 만드는 것이 었습니다.
어떻게 해결할 수 있습니까? 당신이 Add()
를 호출하기 전에 당신이
지금까지 게시 한 내용을 바탕으로
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; }
}
}
해당 엔티티 필드를 채우는 코드를 표시 할 수 있습니까? 내가 뭘 잘못했는지는 알 것 같지만, 그 코드를 볼 때까지 나는 확실히 알지 못할 것이다. – IronMan84
@ IronMan84 예, 선생님. 업데이트 됨. 지금 그것을보십시오. – shanke
페이지 클래스는 어디에 있습니까? –