2016-10-16 5 views
0

새 다 대다 오브젝트를 추가하면 이전 레코드의 일부 관계 필드 값이 null 또는 0으로 설정됩니다 (참조 : 이미지 1).새로운 many-to-Many 오브젝트를 추가 한 후 이전 레코드가 변경됩니다 (바람직하지 않습니다).

Quotes Table

또한 일부 이전 중간 테이블 레코드가 삭제된다 (참조 : 그림 2).

모델 :

public class ModelBase 
{ 
    [PrimaryKey, AutoIncrement]   
    public int Id {set; get;} 
} 


[Table(nameof(Quoter))] 
public class Quoter : ModelBase 
{ 
    [Unique, MaxLength(30)] 
     public string Name {set; get;} 

     [ManyToMany(typeof(QuoterProfession), CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead)] 
     public List<Profession> Professions {set; get;} 

     [OneToMany(CascadeOperations = CascadeOperation.All)] 
     public List<Quote> Quotes {set; get;} 
} 


[Table(nameof(Quote))] 
public class Quote : ModelBase 
{ 
     public string Statement {set; get;} 

     [ForeignKey(typeof(Quoter))] 
     public int QuoterId { get; set; } 
     [ManyToOne(CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead)] 
     public Quoter Quoter {set; get;} 

     [ForeignKey(typeof(Profession))] 
     public int ProfessionId { get; set; } 
     [ManyToOne(CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead)] 
     public Profession Profession {set; get;} 
} 


[Table(nameof(Profession))] 
public class Profession : ModelBase 
{ 
     [Unique, MaxLength(20)] 
     public string Name {set; get;} 

     [ManyToMany(typeof(QuoterProfession), CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead)] 
     public List<Quoter> Quoters {set; get;} 

     [OneToMany(CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead)] 
     public List<Quote> Quotes {set; get;} 
} 


[Table(nameof(QuoterProfession))] 
class QuoterProfession : ModelBase 
{ 
     [ForeignKey(typeof(Quoter))] 
     public int QuoterId { get; set; } 
     [ForeignKey(typeof(Profession))] 
     public int ProfessionId { get; set; } 
} 

뷰 모델 :

public class AddQuoterPageVm 
{ 
    public Quoter Quoter { get; set; } 

     public List<Profession> Professions { get; set; } 

     public ObservableCollection<Profession> QuoterProfessions { get; set; } 
     public Profession QuoterProfession { get; set; } 

     public Profession QuoteProfession { get; set; } 

     public Quote Quote { get; set; } 

     public ObservableCollection<Quote> Quotes { get; set; } 

    public void SaveQuoter() // Add a new Quoter with all his properties & Save 
    { 
     Quoter = new Quoter {Name = "QuoterN"}; 
     asyncConnection.InsertWithChildrenAsync(Quoter).Wait(); // Adds a new Quoter to database 


     AddProfessions(); // Add profession to database 
     Professions = asyncConnection.GetAllWithChildrenAsync<Profession>().Result; 

     // Add QuoterN's profession 
    QuoterProfessions = new ObservableCollection<Profession>{ Professions[0], Professions[1] }; 
     Quoter.Professions = new List<Profession>(QuoterProfessions); 

     Quotes = new ObservableCollection<Quote> 
     { 
      new Quote {Statement = "q1 q1", Profession = Professions[0]}, 
      new Quote {Statement = "q1 q2", Profession = Professions[1]} 
     }; 
    Quoter.Quotes = new List<Quote>(Quotes); // Add QuoterN's quotes 
      Quotes = null; 

    // Update QuoterN in database after adding additional properties 
     asyncConnection.InsertOrReplaceWithChildrenAsync(Quoter, true); 
    } 

    private void AddProfessions() // Adds professions to database 
    { 
     asyncConnection.GetAllWithChildrenAsync() 
        .ContinueWith(professionListTask => 
       { 
        if (professionListTask.Result.Any()) return; 

        var professions = new List<Profession> 
        { 
         new Profession {Name = "Profession1"}, 
         new Profession {Name = "Profession2"}, 
         new Profession {Name = "Profession3"}, 
         new Profession {Name = "Profession4"}, 
         new Profession {Name = "Profession5"}, 
         new Profession {Name = "Profession6"}, 
         new Profession {Name = "Profession7"} 
        }; 

        asyncConnection.InsertAllWithChildrenAsync(professions); 
       }); 
    } 
} 

어떻게 (바람직하지 않다) 이전 기록에 영향을주지 않고 Quoter을 추가 할 수 있습니다 여기에

QuoterProfession intermediate table

코드인가?

답변

0

asyncConnection.UpdateWithChildren(Quoter)을 사용하여 쿼터를 업데이트했습니다. 사용 후이 오래된 레코드는 삭제되지 않습니다.

이전에 asyncConnection.InsertOrReplaceWithChildrenAsync(Quoter, true)을 사용하고있었습니다.

Reference

0

두 관계 모두에서 재귀 적으로 발생하는 것은 문제를 일으키는 원인 일 수 있습니다. 그 중 하나만 설정하고 다른 끝은 일관성이 없기 때문일 수 있습니다. 재귀 삽입 작업을 사용하지 않으므로 모든 관계에서 CascadeOperation.CascadeInsert을 제거하거나 역 관계를 ReadOnly으로 표시하는 것이 좋습니다.

대기중인 비동기 작업으로 인해 문제가 발생할 수 있는지 잘 모르겠습니다. 문제를 해결하기 위해 노력하고 있습니다. 비동기 작업을 기다리는 그 결과 작업을 반환하는 대신 voidTask을 돌아보십시오 :

public async Task SaveQuoter() { 

    Quoter = new Quoter {Name = "QuoterN"}; 
    await asyncConnection.InsertWithChildrenAsync(Quoter); 
    await AddProfessions(); 
    Professions = await asyncConnection.GetAllWithChildrenAsync<Profession>(); 
    ... 
} 

당신은 소스에 포함 된 IntegrationTests project의 샘플 코드를 확인할 수 있습니다.

+0

나는 2 가지 변경을 시도했다. 여전히 문제가 있습니다. – Anil