2

我对 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");)。

已删除的对象将通过级联从关联中删除已删除的对象重新保存

我应该怎么做才能解决这个问题?

4

1 回答 1

2

刷新对数据库的更改,提交事务?

于 2010-12-10T10:22:50.473 回答