ConfORM Nhibernate OneToMany 级联

发布于 2024-10-06 19:52:14 字数 1729 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-10-13 19:52:14

将更改刷新到数据库,提交事务?

Flush changes to DB, Commit transaction?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文