NHibernate - 如何从多对多关系中删除项目?
我跟踪了两个表之间具有多对多关系的映射。如何从映射表中删除条目(在我的例子中为“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();
}
编辑:按照答案中的建议将 Cascade 更改为 SaveUpdate。这是我用来将数据提交到 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);
}
每当我尝试保存/更新时,都会收到以下错误:
具有相同标识符值的不同对象已与实体 Models.Project 的会话:10 关联
编辑2:应使用 session.SaveOrUpdate(project) 和 session.Flush() 以避免上述错误。
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?
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. Here is the code I use to commit data to SQLite database.
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
EDIT2: Should use session.SaveOrUpdate(project) and session.Flush() to avoid error stated above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想删除 ProjectUser 条目,而不实际删除另一侧的实体,则需要从
Cascade.All()
更改为Cascade.SaveUpdate()
。目前,如果您从项目中删除用户并保存项目,则会删除
ProjectUser
条目和User
对象。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()
toCascade.SaveUpdate()
.Currently if you removed a user from a project and saved the project it would delete the
ProjectUser
entry and theUser
object.我的猜测是从 Project.Users 集合中删除适当的 User 实体并保存该项目。然后级联将删除“ProjectUser”中的条目。
My guess would be to remove the appropriate User entity from the Project.Users collection and save that project. The cascade would then remove the entry in "ProjectUser".
如果您想删除项目和用户之间的链接(这意味着从连接表 ProjectUser 中删除记录),您应该从非宇宙一侧进行操作。在您的情况下,这意味着您应该从项目的用户集合中删除用户实体:
在我看来,多对多关联的级联。应设置为 SaveUpdate。您不想在删除用户时删除项目。
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. In your case it means you should remove user entity from the Project's Users collection:
In my opinion cascade in many-to-many assoc. should be set to SaveUpdate. You don't want to delete the project when the user is deleted.