我有一个简单的模型,其中一个可以选择保存在表中Player
的唯一整数Selection
Selection
看起来像这样
public class Player
{
private readonly ICollection<Selection> selection;
public Player()
{
this.selection = new List<Selection>();
}
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual string EmailAddress { get; set; }
public virtual IEnumerable<Selection> Selection
{
get
{
return this.selection;
}
}
public virtual void UpdateSelection(IEnumerable<int> balls)
{
this.selection.Clear();
this.selection.AddRange(balls.Select(ball => new Selection { Player = this, Number = ball }));
}
}
public class Selection
{
public virtual Player Player { get; set; }
public virtual int Number { get; set; }
}
这就是我用来将选择映射到播放器的方法
this.Bag(
x => x.Selection,
m =>
{
m.Key(k => k.Column("PlayerId"));
m.Access(Accessor.Field);
m.Cascade(Cascade.All);
m.Inverse(true);
},
k => k.OneToMany());
并且选择图有一个组合ID
this.ComposedId(
x =>
{
x.Property(y => y.Number);
x.ManyToOne(y => y.Player, m => m.ForeignKey("PlayerId"));
});
当我使用then 的UpdateSelection
方法更改此列表时,表应在必要时插入和删除行。Player
Selection
但是,如果我要从调用 Session.Update 方法时删除一个数字,Selection
我会收到以下错误
Duplicate entry '1-1' for key 'PRIMARY'Could not execute command: INSERT INTO Selection (PlayerId, Number) VALUES (?p0, ?p1)
这是不言自明的,但修复它不是。为什么它试图重新添加数字,不应该只是删除我删除的数字吗?
更新
我将映射从 Bag 更改为 Set,现在我没有收到重复键错误。但是,它不会删除我从列表中删除的号码。