我在 NHibernate 中使用新的 Map by Code 片段。我的理解是在 NHibernate 3 中进行了更新,以便单向一对多关系不再在外键上插入 null 然后将其更新为正确的值,只要您在集合上设置 inverse=false 并进行外键不可为空。
我看到的是 NHibernate 现在插入了正确的外键,但它仍然发出一个额外的 UPDATE 将外键设置为插入中使用的值?!?
我在映射中做错了什么吗?(一个用户可以有多个密码。密码对象不会引用我域中的用户。)
mapper.Class<Password>(map =>
{
map.Table("Passwords");
map.Id(x => x.Id, x => { x.Generator(Generators.Native); x.Column("PasswordId"); });
map.Property(x => x.PasswordFormat, x => { x.NotNullable(true); });
map.Property(x => x.Salt, x => { x.Length(100); });
map.Property(x => x.PasswordValue, x => { x.NotNullable(true); x.Length(500); });
map.Property(x => x.CreateDate, x => { x.NotNullable(true); });
});
mapper.Class<User>(map =>
{
map.Table("Users");
map.Id(x => x.Id, x => { x.Generator(Generators.Native); x.Column("UserId"); });
map.Property(x => x.UserName, x => { x.NotNullable(true); x.Length(100); x.UniqueKey("UX_Users_Username"); });
map.Property(x => x.Email, x => { x.Length(100); x.Index("IX_Users_Email"); });
map.Property(x => x.IsAnonymous, x => { x.NotNullable(true); });
map.Property(x => x.IsApproved, x => { x.NotNullable(true); });
map.Property(x => x.LastActivityDate, x => { x.NotNullable(true); });
map.Property(x => x.CreateDate, x => { x.NotNullable(true); });
map.Set(x => x.Passwords, x => { x.Access(Accessor.Field); x.Inverse(false); x.Key(k => { k.Column("UserId"); k.NotNullable(true); k.ForeignKey("FK_Passwords_UserId"); }); x.Cascade(Cascade.All); x.Lazy(CollectionLazy.Lazy); }, x => x.OneToMany());
});
注意:这是使用内置的 NHibernate.Mapping.ByCode,而不是 Fluent NHibernate。