EntityObject.Remove() 不会从数据库中删除记录
我对 Entity Framework 4 比较陌生。
我的项目由 WebApplication 和 WebApplication 组成。类库项目。我必须使用 ADO.Net POCO 实体生成器,因为我在 ClassLibrary 项目中有多个 edmx 文件,其中包含某些常见模型。
在 edmx 中,我有用户、角色和角色的表。 UserRole(仅包含 2 个外键列,UserID 和 RoleID)。实体框架创建了两个模型,即用户和角色,分别具有角色和用户的导航属性。我已从 .edmx 中删除了定义查询,这使我能够将记录添加到 UserRole 表中。
在我的网络表单中,我有以下代码:
插入角色:
User user = new User(iUserID); //initializes the the user object with the user info
user.AddRole(RoleID); //passes in the roleid that needs to be inserted`
删除用户角色:
User user = new User(iUserID); //initializes the the user object with the user info
user.RemoveRole(RoleID); //passes in the roleid that needs to be deleted`
部分用户类的内容(构造函数和 2 个方法):
public User(short UserID)
{
using (SecurityEntities Context = new SecurityEntities())
{
User user = Context.Users.Where(ua => ua.UserID == UserID).Single<User>();
this.UserID = user.UserID;
// etc...
}
}
public void AddRole(short roleID)
{
using (SecurityEntities Context = new SecurityEntities())
{
Role role = Context.Roles.Where(r => r.RoleID == roleID).Single<Role>();
Context.AttachTo("Users", this);
this.Roles.Add(role);
Context.SaveChanges();
}
}
public void RemoveRole(short roleID)
{
using (SecurityEntities Context = new SecurityEntities())
{
Role role = Context.Roles.Where(r => r.RoleID == roleID).Single<Role>();
Context.AttachTo("Users", this);
this.Roles.Remove(role);
Context.SaveChanges();
}
}
我的问题是
- 在 AddRole 方法中,如果我不使用 AttachTo(),则会输入当前用户的重复记录到 User 表中,并将角色插入到其对应的 UserRole 中。为什么attachTo() 会阻止这种情况发生?
- 在RemoveRole方法中,代码运行顺利,没有错误,但表中的记录没有被删除。为什么?
有人可以帮助我吗?
I am relatively new to Entity Framework 4.
My project consists of a WebApplication & ClassLibrary project. I had to use ADO.Net POCO entity generator since I have multiple edmx files with certain common models in between in the ClassLibrary project.
Within an edmx I have tables for User, Role & UserRole (containing only 2 foreign keys columns, UserID & RoleID). Entity Framework has created two models namely User and Role with navigation properties of Roles and Users respectively. I have removed the definingQuery from the .edmx, which enables me to add records into the UserRole table.
In my webform I have this code:
To insert a role:
User user = new User(iUserID); //initializes the the user object with the user info
user.AddRole(RoleID); //passes in the roleid that needs to be inserted`
To delete a user's role:
User user = new User(iUserID); //initializes the the user object with the user info
user.RemoveRole(RoleID); //passes in the roleid that needs to be deleted`
Content of Partial User class (Constructor & 2 methods):
public User(short UserID)
{
using (SecurityEntities Context = new SecurityEntities())
{
User user = Context.Users.Where(ua => ua.UserID == UserID).Single<User>();
this.UserID = user.UserID;
// etc...
}
}
public void AddRole(short roleID)
{
using (SecurityEntities Context = new SecurityEntities())
{
Role role = Context.Roles.Where(r => r.RoleID == roleID).Single<Role>();
Context.AttachTo("Users", this);
this.Roles.Add(role);
Context.SaveChanges();
}
}
public void RemoveRole(short roleID)
{
using (SecurityEntities Context = new SecurityEntities())
{
Role role = Context.Roles.Where(r => r.RoleID == roleID).Single<Role>();
Context.AttachTo("Users", this);
this.Roles.Remove(role);
Context.SaveChanges();
}
}
My questions are
- In the AddRole method, if I don't use AttachTo() a duplicate record of the current user is entered into the User table and the role is inserted into UserRole against it. Why does attachTo() prevent this from happening?
- In the RemoveRole method, the code runs smoothly without error but the record in the table does not get deleted. Why?
Can anybody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这两种情况下,您都错误地使用了实体框架。用户是由不同的上下文创建(然后跟踪)的。然后,您使用不同的上下文实例检索角色。
要么向方法提供创建
user
的上下文实例,要么向方法提供
Role
实例。Remove方法也会遇到类似的问题,
重要的是使用属于单个上下文实例。否则,您将必须与先前的上下文分离并附加到当前的上下文。
You are using entity framework incorrectly in both cases. The user is created by (then tracked) a different context. Then you retrieve the role using different context instance.
Either supply the context instance which created the
user
to the methodor supply the
Role
instance to the methodRemove method also suffer similar issue
Important thing is to use the entities that belong to single context instance. Otherwise you will have to detach from previous context and attach to the current context.