简体   繁体   English

删除NHibernate映射对象不起作用-导致SQL更新

[英]Deleting NHibernate mapped object not working - causes sql UPDATE

I'm having some trouble with NHibernate, while trying to remove an object describing a relation between rows in two different tables - a many to many relation. 我在尝试删除描述两个不同表中行之间的关系的对象时遇到麻烦,NHibernate遇到了一些麻烦-多对多关系。

I've got Users, CourseInstances and UserRoles, where Users are mapped to certain course instances. 我有用户,课程实例和用户角色,其中用户被映射到某些课程实例。 Users can be a part of many course instances, and course instances have many users attending. 用户可以是许多课程实例的一部分,并且课程实例有许多用户参加。

All this is mapped by Fluent. 所有这些都是Fluent映射的。

User: HasMany(x => x.UserRoles).KeyColumn("UserId");
CourseInstance: HasMany(x => x.UserRoles);
UserRole:
    References(x => x.User).Column("UserId");
    References(x => x.CourseInstance).Column("InstanceId");

The above describes how they are mapped together, its a fairly simple mapping, though each have some additional information but nothing related to each other. 上面描述了如何将它们映射在一起,这是一个相当简单的映射,尽管每个都有一些附加信息,但彼此之间没有任何关系。

I try to run the following code: 我尝试运行以下代码:

using (var session = factory.OpenSession())
{
    var user = session.Get<NData.User>(userId);
    if (user == null)
        throw new FaultException(new FaultReason("No user with that id found."));
    var instance = session.Get<NData.CourseInstance>(courseInstanceId);
    if (instance == null)
        throw new FaultException(new FaultReason("No course instance with that id found."));
    var userrole = session.CreateQuery(string.Format("from UserRole where User.Id = {0} and CourseInstance.Id = {1} and Role.Role = {2}", userId, courseInstanceId, role)).UniqueResult<NData.UserRole>();
    if (userrole == null)
        throw new FaultException(new FaultReason("That user is not present in that course instance with that role."));
    instance.UserRoles.Remove(userrole);
    user.UserRoles.Remove(userrole);
    session.Delete(userrole);
    session.Update(user);
    session.Update(instance);
    session.Flush();
}

However, Flush throws an exception: 但是,Flush引发异常:

could not delete collection rows: [Giraffe.WebService.NHibernate.Data.User.UserRoles#8][SQL: UPDATE UserRoles SET UserId = null WHERE UserId = @p0 AND Id = @p1]

Why is it trying to UPDATE the role first? 为什么要尝试首先更新角色?

Because you're doing 因为你在做

user.UserRoles.Remove(userrole);

user is a managed entity that NHibernate will update, just as you're seeing. 用户是NHibernate将更新的托管实体,正如您所看到的。

It is updating UserRoles first because that is what you do first: 它首先更新UserRoles,因为这是您首先要做的:

instance.UserRoles.Remove(userrole);
user.UserRoles.Remove(userrole);

Instead, delete the role before removing from associations. 相反,请删除角色,然后再将其从关联中删除。

Also, there is no need for: 另外,也不需要:

session.Update(user);
session.Update(instance);

They will be updated when the session is flushed. 刷新会话后将更新它们。 Moreover, this is invalid because Update is meant to be used with disassociated entities. 而且,这是无效的,因为Update是与分离的实体一起使用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM