2015-01-15 11 views
1

하나의 ReportRow 개체에 개체 목록 (ReportCell)을 보유하기 위해 Entity Framework를 사용하고 있습니다. 그런 다음 여러 개의 ReportRow 개체가 하나의 보고서에 보관됩니다.hasMany 관계를 지정할 때 다중성 제약 조건 위반

보고서

public class Report 
{ 
    public int ReportId { get; set; } 
    public virtual List<ReportRow> ReportRows { get; set; } 

    public Report() 
    {    
     ReportRows = new List<ReportRow>(); 
    } 
} 

ReportRow

public class ReportRow 
{ 
    public int ReportRowId { get; set; } 
    public virtual List<ReportCell> ReportCells { get; set; } 

    public ReportRow() 
    { 
     ReportCells = new List<ReportCell>(); 
    } 
} 

ReportCell

:

클래스는 다음과 같다

public class ReportCell 
{ 
    public int ReportCellId { get; set; } 
    public string Data { get; set; } 
} 
그런 다음 엔티티 프레임 워크에 나는 코드 먼저 이러한 클래스를 유지하기 위해 프레임 워크 알려주기 :
modelBuilder.Entity<Report>().HasKey(r => r.ReportId); 
modelBuilder.Entity<Report>() 
      .Property(r => r.ReportId) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
modelBuilder.Entity<Report>().HasMany(rep => rep.ReportRows); 

modelBuilder.Entity<ReportRow>().HasKey(rr => rr.ReportRowId); 
modelBuilder.Entity<ReportRow>() 
      .Property(rr => rr.ReportRowId) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
modelBuilder.Entity<ReportRow>().HasMany(rr => rr.ReportCells); 

modelBuilder.Entity<ReportCell>().HasKey(rc => rc.ReportCellId); 
modelBuilder.Entity<ReportCell>() 
      .Property(rc => rc.ReportCellId) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
modelBuilder.Entity<ReportCell>().Property(rc => rc.Data); 

내가 성공적으로 마이그레이션 한 후 데이터베이스를 채 웁니다 광범위한 데이터베이스 씨가, 엔티티 프레임 워크가 던져 관리하기 때문에 내 모든 모든 이전 데이터 ( AutomaticDataLossAllowed = true). 다음 코드를 실행하면 , 나는 SaveChanges를()
protected override void Seed(ReportModuleContext context) 
{ 
    var cells = new List<ReportCell>(); 
    for (var n = 1; n <= 100; n++) 
    { 
      cells.Add(new ReportCell { Data = "Random data " + n, ReportCellId = n}); 
    } 

    var rows = new List<ReportRow>(); 
    for (var x = 0; x < 20; x++) 
    { 
     var row = new ReportRow(); 
     row.ReportCells.Add(cells[x]); 
     row.ReportCells.Add(cells[x+1]); 
     row.ReportCells.Add(cells[x+2]); 
     row.ReportCells.Add(cells[x+3]); 
     row.ReportCells.Add(cells[x+4]); 
     row.ReportRowId = x; 
     rows.Add(row); 
    } 

    IList<Report> reports = new List<Report>(); 
    reports.Add(new Report 
    { 
     ReportId = 1, 
     ReportRows = rows 
    }); 
    reports.Add(new Report 
    { 
     ReportId = 2, 
     ReportRows = rows 
    }); 
    foreach (var rep in reports) 
    { 
     context.Reports.AddOrUpdate(rep); 
    } 
    context.SaveChanges(); 
} 

는 그래서이하는 모든 Data로 사람들을 사용, 일부 문자열을 각 행에 5 개 셀을 추가입니다에 예외가. 그런 다음 보고서에 5 개의 행을 추가하십시오. 그런 다음 Entity Framework가 튀어 나오기 시작합니다. 예외 :

다중성 제약 조건 위반. 'Persistance.ReportRow_ReportCells'관계의 'ReportRow_ReportCells_Source'역할은 다중성 1 또는 0..1을가집니다.

오류에 대한 이상한 것은 내가 Report 사이의 hasMany()를 사용하고 있다는 점이다 (나는 ... 전에 물어 사용할 수있는 모든 질문에 보았다 아직 현재로 만족스러운 답변을 찾지 못했다) ReportRow 그게 잘 작동합니다. 하지만 ReportRowReportCell 사이에서 동일한 작업이 수행되지 않습니까? 나는 정말로 단순한 무언가가 빠져 있기를 바란다. 나는 이미 하루를 잊어 버렸다. ...

+0

당신이'cells'에 각 조회에 두 번째 루프에서 5 x''곱하려는나요 : 같은

고정 코드가 보인다? 왜냐하면 현재 5 개의 다른 행 (셀에있는 첫 번째 5 개의 행, 0과 4 사이의'x '값)에 셀 [4]를 추가하려고하기 때문입니다. 그런 다음 동일한 보고서를 여러 보고서에 할당하는 별도의 (그러나 비슷한 문제가 발생했다고 생각합니다.) –

+0

젠장, 네 말이 맞아 ... 편집 : AAaaaaand 그것은 작동합니다. 와우. 고마워요! –

답변

0

해결책은 Damien_The_Unbeliever에 의해 제공되었다.

두 개의 서로 다른 개체에서 동일한 개체를 사용하면 Entity Framework에서 문제가 발생하기 시작합니다. for 루프의 작은 실수로 인해 객체가 두 번 이상 사용되었습니다. 루프를 고정하면 문제가 완전히 해결되었습니다.

var rows = new List<ReportRow>(); 
for (var x = 0; x < 20; x+=5) 
{ 
    var row = new ReportRow(); 
    row.ReportCells.Add(cells[x]); 
    row.ReportCells.Add(cells[x+1]); 
    row.ReportCells.Add(cells[x+2]); 
    row.ReportCells.Add(cells[x+3]); 
    row.ReportCells.Add(cells[x+4]); 
    row.ReportRowId = x; 
    rows.Add(row); 
}