2

考慮下面的模型/映射貧嘴多到許多不更新數據庫時添加子

[DataContract] 
public class CustomPhrase : ModelBase 
{ 
    private ICollection<EnglishPhrase> translations; 

    public CustomPhrase() 
    { 
    this.translations = new List<EnglishPhrase>(); 
    } 

    [DataMember] 
    public virtual string Phrase { get; set; } 

    [DataMember] 
    public virtual IEnumerable<EnglishPhrase> Translations 
    { 
    get 
    { 
     return this.translations; 
    } 
    } 
} 

[DataContract] 
public class EnglishPhrase : ModelBase 
{ 
    private ICollection<CustomPhrase> translations; 

    public CustomPhrase() 
    { 
    this.translations = new List<CustomPhrase>(); 
    } 

    [DataMember] 
    public virtual string Phrase { get; set; } 

    [DataMember] 
    public virtual IEnumerable<CustomPhrase> Translations 
    { 
    get 
    { 
     return this.translations; 
    } 
    } 
} 

public CustomPhraseMap() : base("Translation_CustomPhrase", "CustomPhraseId") 
{ 
    this.Property(x => x.Phrase); 
    this.Set(
    x => x.Translations, 
    map => 
     { 
     map.Table("Translation_Link"); 
     map.Key(key => key.Column("CustomPhraseId")); 
     map.Access(Accessor.Field); 
     map.Cascade(Cascade.All); 
     }, 
    map => map.ManyToMany(c => c.Column("EnglishPhraseId")) 
    ); 
} 

public EnglishPhraseMap() : base("Translation_EnglishPhrase", "EnglishPhraseId") 
{ 
    this.Property(x => x.Phrase); 
    this.Set(
    x => x.Translations, 
    map => 
     { 
     map.Table("Translation_Link"); 
     map.Key(key => key.Column("EnglishPhraseId")); 
     map.Access(Accessor.Field); 
     map.Cascade(Cascade.All); 
     }, 
    map => map.ManyToMany(c => c.Column("CustomPhraseId")) 
    ); 
} 

如果我執行這段代碼,實體被添加到數據庫中,但鏈接表Translation_Link不更新。

var customPhrase = new CustomPhrase { Phrase = "Gobble" }; 
var englishPhrase = new EnglishPhrase { Phrase = "Hello" }; 
customPhrase.AddTranslation(englishPhrase); 
this.customPhraseRepository.Add(customPhrase); 

我不確定這是映射問題還是存儲庫配置問題。也許我在錯誤的時間添加了孩子?

有沒有其他人遇到過這個問題,並能夠解決它?

回答

1

我設法使用事務來解決這個問題。

try 
    { 
    unitOfWork.BeginTransaction(); 

    var custom = this.customPhraseRepository.FindCustomPhraseByPhrase(customPhrase); 
    if (custom == null) 
    { 
     custom = new CustomPhrase() { Phrase = customPhrase }; 
     this.customPhraseRepository.Add(custom); 
    } 

    var english = this.englishPhraseRepository.FindEnglishPhraseByPhrase(englishPhrase); 
    if (english == null) 
    { 
     english = new EnglishPhrase() { Phrase = englishPhrase }; 
     this.englishPhraseRepository.Add(english); 
    } 

    custom.AddTranslation(english); 
    this.customPhraseRepository.Update(custom); 

    unitOfWork.EndTransaction(); 
    } 
    catch (Exception) 
    { 
    unitOfWork.RollBack(); 
    } 
    finally 
    { 
    unitOfWork.Dispose(); 
    } 
1

您需要爲兩側的集合設置「反」爲TRUE。

+0

這也是我最初的反應。我在這兩個地圖中都放入了Inverse(),並且得到以下錯誤 - 「{」CustomPhrase.Translations和EnglishPhrase.Translations之間的關係在兩側都有指定的反向。從關係的一側刪除反向。「}'Remove a one .Inverse()函數的結果仍然相同。 – JConstantine