2

Consider the following models/mappings

[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"))
    );
}

If I execute this bit of code, the entities are added to the database but the link table Translation_Link is not updated.

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

I'm not sure if this is a mapping problem, or a repository configuration problem. Maybe I'm adding the child at the wrong time??

Has anyone else come across this problem before and been able to fix it?

Daniel Schilling
  • 4,829
  • 28
  • 60
JConstantine
  • 3,980
  • 1
  • 33
  • 46

2 Answers2

1

You need to set the inverse to TRUE for the collections on both sides.

TWith2Sugars
  • 3,384
  • 2
  • 26
  • 43
  • that was my initial reaction as well. I put .Inverse() in both maps, and got the following error - `{"The relationship CustomPhrase.Translations to EnglishPhrase.Translations has Inverse specified on both sides. Remove Inverse from one side of the relationship."}` Having removed one of the .Inverse() functions the outcome is still the same. – JConstantine Aug 20 '11 at 09:59
1

I managed to fix this problem using a transaction.

  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();
  }
JConstantine
  • 3,980
  • 1
  • 33
  • 46