简体   繁体   English

NHibernate - 如何从多对多关系中删除项目?

[英]NHibernate - how to delete an item from many-to-many relationship?

I've following mapping for two tables having a Many-to-Many relationship between them. 我跟踪两个表之间的映射,它们之间具有多对多关系。 How do I delete an entry from the mapping table, which is 'ProjectUser' in my case? 如何从映射表中删除一个条目,在我的情况下是“ProjectUser”?

public ProjectMap()
{
    Id(x => x.Id);
    Map(x => x.ProjectName);
    Map(x => x.Description);
    References<User>(x => x.Owner);
    HasManyToMany(x => x.Users)
        .Cascade.SaveUpdate()
        .Table("ProjectUser")
        .Not.LazyLoad();
}


public UserMap()
{
    Id(x => x.Id);
    Map(x => x.FirstName);
    Map(x => x.LastName);
    Map(x => x.UserName);
    HasManyToMany(x => x.Projects)
        .Cascade.SaveUpdate()
        .Inverse()
        .Table("ProjectUser")
        .Not.LazyLoad();
}

EDIT: changed Cascade to SaveUpdate as suggested in answers. 编辑:按照答案中的建议将Cascade更改为SaveUpdate。 Here is the code I use to commit data to SQLite database. 这是我用来将数据提交到SQLite数据库的代码。

using (var trans = session.BeginTransaction())
{
    var existingUsers = project.Users.ToList();
    foreach (var item in existingUsers)
    {
        if (selectedUsers.Count(x => x.Id == item.Id) == 0)
            project.Users.RemoveAt(project.Users.IndexOf(item));
    }
    session.SaveOrUpdate(project); // This fixed the issue
    session.Flush();
    foreach (var item in selectedUsers)
    {
        if (project.Users.Count(x => x.Id == item.Id) == 0)
        {
            project.AddUser(session.Get<User>(item.Id));
        }
    }
    session.SaveOrUpdate(project);
    session.Flush();
    trans.Commit();
}

// Add user code in Project class
public virtual void AddUser(User userToAdd)
{
    if (this.Users == null)
        this.Users = new List<User>();
    userToAdd.Projects.Add(this);
    this.Users.Add(userToAdd);
}

Whenever I try to save/update I'm getting the following error: 每当我尝试保存/更新时,我都会收到以下错误:

a different object with the same identifier value was already associated with the session: 10, of entity: Models.Project 具有相同标识符值的不同对象已与会话关联:10,实体:Models.Project

EDIT2: Should use session.SaveOrUpdate(project) and session.Flush() to avoid error stated above. EDIT2:应使用session.SaveOrUpdate(项目)和session.Flush()来避免上述错误。

If you only want to remove the ProjectUser entry and not actually delete the entity on the other side you need to change from Cascade.All() to Cascade.SaveUpdate() . 如果您只想删除ProjectUser条目而不是实际删除另一侧的实体,则需要从Cascade.All()更改为Cascade.SaveUpdate()

Currently if you removed a user from a project and saved the project it would delete the ProjectUser entry and the User object. 目前,如果您从项目中删除了用户并保存了项目,则会删除ProjectUser条目和User对象。

My guess would be to remove the appropriate User entity from the Project.Users collection and save that project. 我的猜测是从Project.Users集合中删除相应的User实体并保存该项目。 The cascade would then remove the entry in "ProjectUser". 然后级联将删除“ProjectUser”中的条目。

If you want to remove link between a Project and an User (it means removing record from the join table ProjectUser) you should act from noniverse side. 如果要删除项目和用户之间的链接(这意味着从连接表ProjectUser中删除记录),您应该从noniverse端执行操作。 In your case it means you should remove user entity from the Project's Users collection: 在您的情况下,这意味着您应该从Project的Users集合中删除用户实体:

project.Users.Remove(user);

In my opinion cascade in many-to-many assoc. 在我看来,在多对多关联中级联。 should be set to SaveUpdate. 应设置为SaveUpdate。 You don't want to delete the project when the user is deleted. 删除用户时,您不想删除项目。

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

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