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