ConfORM Nhibernate OneToMany 级联
我对 Nhibernate ConfORM 级联有一些问题。 存在两个实体:
public class User : BaseEntity
{
public User(Role role)
{
this.Role = role;
}
protected User()
{
}
public virtual Role Role { get; protected set; }
}
和 角色:
public class Role : BaseEntity
{
public Role()
{
this.Users = new HashedSet<User>();
}
public virtual ISet<User> Users { get; protected set; }
}
级联设置:
var relationalMapper = new ObjectRelationalMapper();
relationalMapper.TablePerConcreteClass(domainEntities);
relationalMapper.Patterns.PoidStrategies.Add(new NativePoidPattern());
relationalMapper.Cascade<User, Role>(Cascade.Persist);
relationalMapper.Cascade<Role, User>(Cascade.Persist);
此自动映射提供:
<class name="User" table="Users">
<id name="Id" type="Int32">
<generator class="native" />
</id>
<many-to-one name="Role" column="RoleId" cascade="save-update, persist" />
我想删除用户而不删除角色。 几乎所有测试都已通过,但这个测试失败了:
this.role = new Role();
this.user = new User(this.role) { Login = "memoryUser", Password = "memoryPass" }.SetIdTo(0);
role.Users.Add(user);
this.session.SaveOrUpdate(this.user);
repo.Delete(this.user);
var u = repo.GetByName("memoryUser");
u.Should().Be.Null();
错误消息: 当我尝试获取已删除的用户时,它会出现(var u = repo.GetByName("memoryUser");)。
删除的对象将通过级联重新保存,从关联中删除已删除的对象
我应该做什么来解决这个问题?
I have some problem with Nhibernate ConfORM cascade.
Exist two entities:
public class User : BaseEntity
{
public User(Role role)
{
this.Role = role;
}
protected User()
{
}
public virtual Role Role { get; protected set; }
}
and Role:
public class Role : BaseEntity
{
public Role()
{
this.Users = new HashedSet<User>();
}
public virtual ISet<User> Users { get; protected set; }
}
Cascade settings:
var relationalMapper = new ObjectRelationalMapper();
relationalMapper.TablePerConcreteClass(domainEntities);
relationalMapper.Patterns.PoidStrategies.Add(new NativePoidPattern());
relationalMapper.Cascade<User, Role>(Cascade.Persist);
relationalMapper.Cascade<Role, User>(Cascade.Persist);
This automapping gives:
<class name="User" table="Users">
<id name="Id" type="Int32">
<generator class="native" />
</id>
<many-to-one name="Role" column="RoleId" cascade="save-update, persist" />
I want to delete User without delete Role.
All most all tests have passed, but this one falls:
this.role = new Role();
this.user = new User(this.role) { Login = "memoryUser", Password = "memoryPass" }.SetIdTo(0);
role.Users.Add(user);
this.session.SaveOrUpdate(this.user);
repo.Delete(this.user);
var u = repo.GetByName("memoryUser");
u.Should().Be.Null();
Error message:
It appears when I try to get deleted users (var u = repo.GetByName("memoryUser");).
deleted object would be re-saved by cascade remove deleted object from associations
What should I do to fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将更改刷新到数据库,提交事务?
Flush changes to DB, Commit transaction?