Subsonic 3 简单存储库多对多关系
我在之前的一篇文章这里在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从来没有真正为这种事情编写过 linq 查询,因为我宁愿使用内置的活动目录存储库。如果您想走这条路:
我首先要确保您的数据库配置了适当的外键:
如果正确完成,Subsonic3 将为您生成创建 IQueryable 子对象的类。如果您想要获取附加了用户的所有角色,您还需要将 UsersAndRoles 列表添加到角色对象中。 您必须以不同的方式命名它,因为您不能将其命名为与 IQueryable 对象相同的名称:
现在要获取附加了用户的所有角色,请执行以下操作:
访问所有角色的所有用户:
当然这是徒手的,所以肯定会出现编译错误。让我知道这是否有帮助。祝你好运!
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:
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:
Now to get all roles with the users attached, do this:
to access all users for all roles:
Of course this is freehand so there are bound to be compile errors. Let me know if this helps or not. Good luck!
Weel,因为我想在没有任何数据库查找的情况下保持我的实体干净,所以我不能像你建议的那样这样做。
我创建了这样的实体:
角色实体
用户实体
用户和角色实体
现在,从我的存储库中,我尝试执行以下操作:
但是,即使我可以在单步执行时加载用户,用户也不会添加到角色中使用调试代码。
我在这里缺少什么?
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
User entity
Users and roles entity
Now from my repository I try to do the folowing :
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 ?