添加子项时,多对多不更新数据库
考虑以下模型/映射
[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);
我不确定这是映射问题还是存储库配置问题。也许我在错误的时间添加了孩子?
有其他人以前遇到过这个问题并能够解决它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将两侧的集合的逆设置为 TRUE。
You need to set the inverse to TRUE for the collections on both sides.
我设法使用事务解决了这个问题。
I managed to fix this problem using a transaction.