【问题标题】:Entity Framework 6 Include from string Array实体框架 6 包含来自字符串数组
【发布时间】:2016-05-06 21:23:25
【问题描述】:

我试图通过调用字符串数组中的方法来简化包含语句:

Dim list As New List(Of User)
Using ctx As New UsersEntities
    Dim query = ctx.User
    For Each s As String In relations.Split(",") 'relations = "Role,Gender"
        If s IsNot Nothing And s.Length > 0 Then
            query.Include(s)
        End If
    Next
    list = query.ToList()
End Using
Return list

但是,当返回列表时,它会抛出“System.ObjectDisposedException”

无论如何都可以这样做??我需要使用逗号分隔的字符串动态地告诉 EF 我想要包含哪些“关系”

PS:我尝试禁用 LazyLoad,但 Relations 总是返回 Nothing。

谢谢!!!

【问题讨论】:

  • 首先应该是query = query.Include(s)。其次,我认为错误与包含无关,但您可能会在释放上下文后访问导航属性。
  • 我没有注意到这一点。所以,我尝试添加 query = query.Include (s),但我得到了一个转换异常(无法转换类型为 'System.Data.Entity.Infrastructure.DbQuery1 [User]' to type 'System.Data.Entity. DbSet1 [User]' 的对象。)
  • 我可以解决这个问题!您必须将变量“query”的类型指定给Infraestructure.DBQuery,并且还要修正这句话:query = query.Include(s)。谢谢@GertArnold!!

标签: asp.net vb.net entity-framework include entity-framework-6


【解决方案1】:

解决方案:

必须将变量“query”的类型指定给Infraestructure.DBQuery,并且还要固定一句:query = query.Include(s)

Dim list as new List(Of User)
Using ctx As New UsersEntities
    Dim query As Entity.Infrastructure.DbQuery(Of User) = ctx.User
    For Each s As String In relations.Split(",")
        If s IsNot Nothing And s.Length > 0 Then
            query = query.Include(s)
        End If
    Next
    list = query.ToList()
End Using

【讨论】:

    猜你喜欢
    • 2013-12-18
    • 2021-04-13
    • 2015-08-10
    • 1970-01-01
    • 2021-08-15
    • 2017-09-02
    • 2017-03-28
    • 1970-01-01
    • 2010-12-17
    相关资源
    最近更新 更多