Subsonic 3 简单存储库多对多关系

发布于 2024-08-14 07:12:29 字数 768 浏览 6 评论 0原文

我在之前的一篇文章这里在 stackoverflow 看到了一个关于多对多关系。为了简单起见,让我们看一下这些类:

public class Role(){
    public int Id { get; set; }
    public string Rolename { get; set; }
    public string Description { get; set; }
}

public class User(){
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public IList<UsersAndRoles> UsersAndRoles { get; set; }
}

public class UsersAndRoles(){
    public int Id { get; set; }
    public User User { get; set; }
    public Role Role { get; set; }
}

这是徒手的,所以请耐心等待。

现在对这些类进行 linq 查询的最佳方法是什么?假设我想获取所有附加了用户的角​​色。有人可以帮我解决这个问题吗?

I saw in an earlier post here on stackoverflow a example on many to many relations. To keep it simple, lets have a look at these classes :

public class Role(){
    public int Id { get; set; }
    public string Rolename { get; set; }
    public string Description { get; set; }
}

public class User(){
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public IList<UsersAndRoles> UsersAndRoles { get; set; }
}

public class UsersAndRoles(){
    public int Id { get; set; }
    public User User { get; set; }
    public Role Role { get; set; }
}

This is freehand so bear with me.

Now what is the best way to make the linq query for these classes. Lets say I want to get all roles with the users attached to them. Can someone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谢绝鈎搭 2024-08-21 07:12:29

我从来没有真正为这种事情编写过 linq 查询,因为我宁愿使用内置的活动目录存储库。如果您想走这条路:

我首先要确保您的数据库配置了适当的外键:

Table 'Roles'
int Id (PK)

Table 'Users'
int Id (PK)

Table 'UsersAndRoles'
int RoleId (FK)
int UserId (FK)

如果正确完成,Subsonic3 将为您生成创建 IQueryable 子对象的类。如果您想要获取附加了用户的所有角色,您还需要将 UsersAndRoles 列表添加到角色对象中。 您必须以不同的方式命名它,因为您不能将其命名为与 IQueryable 对象相同的名称

public partial class Role()
{
    public int Id { get; set; }
    public string Rolename { get; set; }
    public string Description { get; set; }

    private IList<UsersAndRoles> _UserToRoleLinks
    public IList<UsersAndRoles> UserToRoleLinks 
    { 
        get
        {
            if(_UserToRoleLinks == null)
                _UserToRoleLinks = this.UsersAndRoles.ToList();
            return _UserToRoleLinks;
        }
    }
}

public partial class UsersAndRoles
{
    private Role _Role;
    public Role Role
    {
        get
        {
            if (_Role == null)
                _Role = this.Roles.SingleOrDefault();
            return _Role;
        }
        set
        {
            _Role = value;
        }
    }

    private User _User;
    public User User
    {
        get
        {
            if (_User == null)
                _User = this.Users.SingleOrDefault();
            return _User;
        }
        set
        {
            _User = value;
        }
    }
}

现在要获取附加了用户的所有角色,请执行以下操作:

var roles = Role.All();

访问所有角色的所有用户:

foreach (var role in roles)
{
    foreach (var userToRoleLink in role.UserToRoleLinks)
    {
        var currentUser = userToRoleLink.User;
    }
}

当然这是徒手的,所以肯定会出现编译错误。让我知道这是否有帮助。祝你好运!

I've never really written linq queries for this sorta thing as I would rather use the built in active directory repository. If you want to go this route:

I would start first by making sure that your database has the appropriate foreign keys configured:

Table 'Roles'
int Id (PK)

Table 'Users'
int Id (PK)

Table 'UsersAndRoles'
int RoleId (FK)
int UserId (FK)

If this is done correctly, Subsonic3 will generate the classes for you creating IQueryable child objects. If you want to get all roles with the user attached, you'll also need to add a list of UsersAndRoles to the Role Object. You'll have to name this differently as you can't name it the same as the IQueryable object:

public partial class Role()
{
    public int Id { get; set; }
    public string Rolename { get; set; }
    public string Description { get; set; }

    private IList<UsersAndRoles> _UserToRoleLinks
    public IList<UsersAndRoles> UserToRoleLinks 
    { 
        get
        {
            if(_UserToRoleLinks == null)
                _UserToRoleLinks = this.UsersAndRoles.ToList();
            return _UserToRoleLinks;
        }
    }
}

public partial class UsersAndRoles
{
    private Role _Role;
    public Role Role
    {
        get
        {
            if (_Role == null)
                _Role = this.Roles.SingleOrDefault();
            return _Role;
        }
        set
        {
            _Role = value;
        }
    }

    private User _User;
    public User User
    {
        get
        {
            if (_User == null)
                _User = this.Users.SingleOrDefault();
            return _User;
        }
        set
        {
            _User = value;
        }
    }
}

Now to get all roles with the users attached, do this:

var roles = Role.All();

to access all users for all roles:

foreach (var role in roles)
{
    foreach (var userToRoleLink in role.UserToRoleLinks)
    {
        var currentUser = userToRoleLink.User;
    }
}

Of course this is freehand so there are bound to be compile errors. Let me know if this helps or not. Good luck!

叫思念不要吵 2024-08-21 07:12:29

Weel,因为我想在没有任何数据库查找的情况下保持我的实体干净,所以我不能像你建议的那样这样做。

我创建了这样的实体:

角色实体

public class Role(){
    public int RoleId { get; set; }
    public string Rolename { get; set; }
    public string Description { get; set; }
    public IList<User> Users { get; set; }
    public Role()
    {
        RoleId = 0;
        Rolename = "";
        Description = "";
        Users = new List<User>();
    }
}

用户实体

public class User(){
    public int UserId { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public User()
    {
        UserId = 0;
        Username = "";
        Password = "";
        Email = "";
    }
}

用户和角色实体

public class UsersAndRoles(){
    public int Id { get; set; }
    public int UserId { get; set; }
    public int RoleId { get; set; }
    public UsersInRoles()
    {
        Id = 0;
        UserId = 0;
        RoleId = 0;
    }
}

现在,从我的存储库中,我尝试执行以下操作:

public IList<Role> GetRolesAndUsers()
{
    //--- Load all roles
    var roles = base.All<Role>();

    //--- Run through all roles and add the users to the roles.
    foreach (var role in roles)
    {
        //--- Load the users in the given role
        var users = (from u in base.All<User>()
                     join ur in base.All<UsersInRoles>() on role.RoleId equals ur.RoleId
                     where u.UserId == ur.UserId
                     select u).ToList();

        //--- Run through the list of users and add the user to the role
        foreach (var user in users)
        {
            role.Users.Add(user);
        }
    }
    //--- Return roles and users
    return roles.ToList();
}

但是,即使我可以在单步执行时加载用户,用户也不会添加到角色中使用调试代码。

我在这里缺少什么?

Weel, as i want to keep my Entities clean without any database lookups I can't do it like you sugested.

I made my entities like this :

Role entity

public class Role(){
    public int RoleId { get; set; }
    public string Rolename { get; set; }
    public string Description { get; set; }
    public IList<User> Users { get; set; }
    public Role()
    {
        RoleId = 0;
        Rolename = "";
        Description = "";
        Users = new List<User>();
    }
}

User entity

public class User(){
    public int UserId { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public User()
    {
        UserId = 0;
        Username = "";
        Password = "";
        Email = "";
    }
}

Users and roles entity

public class UsersAndRoles(){
    public int Id { get; set; }
    public int UserId { get; set; }
    public int RoleId { get; set; }
    public UsersInRoles()
    {
        Id = 0;
        UserId = 0;
        RoleId = 0;
    }
}

Now from my repository I try to do the folowing :

public IList<Role> GetRolesAndUsers()
{
    //--- Load all roles
    var roles = base.All<Role>();

    //--- Run through all roles and add the users to the roles.
    foreach (var role in roles)
    {
        //--- Load the users in the given role
        var users = (from u in base.All<User>()
                     join ur in base.All<UsersInRoles>() on role.RoleId equals ur.RoleId
                     where u.UserId == ur.UserId
                     select u).ToList();

        //--- Run through the list of users and add the user to the role
        foreach (var user in users)
        {
            role.Users.Add(user);
        }
    }
    //--- Return roles and users
    return roles.ToList();
}

However the users does not get added to the roles even though I can se the users get loaded when I step through the code using debug.

What am I missing here ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文