如何删除实体在许多一对多关系使用波苏斯 [英] How to delete entity in many-to-many relationship using POCOs

查看:178
本文介绍了如何删除实体在许多一对多关系使用波苏斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在EF4结合使用波苏斯和一些实体在许多一对多的关系,在课堂上用户和类PrivilegeGroup的对象,我的情况的对象。

I'm using POCOs in combination with EF4 and some entities are in many-to-many relationships, in my case objects of class User and objects of class PrivilegeGroup.

这是类用户的样子:

public class User
{
    public int UserID { set; get; }
    public string UserName { get; set; }
    public string UserPassword { get; set; }
    public bool IsActive { get; set; }

    public List<PrivilegeGroup> PrivilegeGroups { get; set; }
}



这是类PrivilegeGroup的样子:

And this is how class PrivilegeGroup looks like:

public class PrivilegeGroup
{
    public int PrivilegeGroupID { get; set; }
    public string Name { get; set; }

    public List<User> Users { get; set; }
    public List<HasPrivilege> HasPrivileges { get; set; }
}



我已经延长ObjectContext类
,如下所示:

I have extended ObjectContext class as follows:

public class AdminMDSContext : ObjectContext
{

    public AdminMDSContext(string connectionString)
        : base(connectionString)

    {
        this.DefaultContainerName = "MDSUsers_Entities";
        _users = CreateObjectSet<User>();
        _privilegeGroups = CreateObjectSet<PrivilegeGroup>();

    }


    private ObjectSet<User> _users;
    private ObjectSet<PrivilegeGroup> _privilegeGroups;

    public ObjectSet<User> Users
    {
        get { return _users; }
    }


    public ObjectSet<PrivilegeGroup> PrivilegeGroups
    {
        get { return _privilegeGroups; }
        set { _privilegeGroups = value; }
    }

}



查询和这些实体的插入工作正常,但缺失使得问题,即我想从一个用户无需分贝往返删除PrivilegeGroup,但我不知道该怎么办了。

Querying and insertion of these entities are working fine, but deletion is making problem, i.e. I want to remove PrivilegeGroup from one User without db roundtrip, but I don't know how to do it.

灿有人帮帮我吗?

推荐答案

有趣的问题。这里为u如何做到这一点。

Interesting question. Here is how u do it.

var user = new User { UserId = 1 };
var admin = new Privilege { PrivilegeId = 1 };
user.Privileges.Add(admin);
db.Users.Attach(user);
user.Privileges.Remove(admin);
db.SaveChanges();

有共计4种不同的方法来解决同样的问题。但是我觉得从什么ü告诉我,这应该足够了,但如果u需要更多的信息,你可以直接ping通我通过邮件

There are total of 4 different approaches to solving the same problem. but i think from what u are telling me, this should suffice but if u need more info, you can ping me directly through mail

这篇关于如何删除实体在许多一对多关系使用波苏斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆