当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - Entity Framework and include

问题描述:

I have created a edmx model of the aspnet login account holders tables.

Entityframework removes the AspNetUserRoles table and display a relationship directly between AspNetRole and the AspNetUser tables

The question I have is how or what is the best why to get a list of AspNetUser in a certain role role?

Public Function getAccountHoldersInRoles(role As String) As IEnumerable(Of AspNetUser)

Dim aspNetUsers = From u In db.AspNetUsers.Include("AspNetRoles")

For Each u In aspNetUsers

??????????????

Next

Return aspNetUsers

End Function

网友答案:

You don't need the Include unless you have some case where you need the data after the context is disposed.

If Users and Roles have a many-to-many relationship then this is what you need.

C#

var usersInRole = db.AspNetUsers.Where(u => u.Roles.Any(r => r.RoleName == role));

VB

Dim usersInRole = db.AspNetUsers.Where(Function(u) u.Roles.Any(Function(r) r.RoleName = role))
分享给朋友:
您可能感兴趣的文章:
随机阅读: