EntiryFramwork CodeFirst与GridView [英] EntiryFramwork CodeFirst with GridView

查看:99
本文介绍了EntiryFramwork CodeFirst与GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是实体框架的新手,发现CodeFirst版本还可以.因此,我决定创建一个简单的asp.net Webforms应用程序进行测试.用一些数据填充表,并尝试使用以下代码检索数据:

Hi,

I am new to entity framework and found that the CodeFirst release is okay. So I decided to create a simple asp.net webforms application to test this. Populated a table with some data and tried to retrieve the data with the following code:

Imports System.Data.Entity

Public Class AdmonHRMDbContext
    Inherits DbContext

    Public Property Application_User As DbSet(Of User)
End Class







Public Class User
    Public Property UserId As Int32
    Public Property LoginName As String
    Public Property Password As String
End Class





Public Class UserRepository

    Private context As AdmonHRMDbContext

    Public Sub New()
        context = New AdmonHRMDbContext
    End Sub

    Public Function GetUsers() As IQueryable(Of User)
        Return context.Application_User
    End Function
End Class









Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim u As New BusinessLogic.UserRepository()

    GridView1.DataSource = u.GetUsers
    GridView1.DataBind()
End Sub




以下是我得到的例外:
不支持将数据直接绑定到商店查询(DbSet,DbQuery,DbSqlQuery).而是使用数据填充DbSet,例如,通过在DbSet上调用Load,然后将其绑定到本地数据.对于WPF,请绑定到DbSet.Local.对于WinForms,绑定到DbSet.Local.ToBindingList().

我不知道该怎么办,请帮忙

Aweklin




below is the exception I got:
Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().

I don''t know what to do please help

Aweklin

推荐答案

您无法绑定到IQueryable,因为它仅描述了如何检索和检索哪些数据.您必须首先实际查询数据,例如通过使用ToList扩展方法.

you can''t bind to IQueryable as it only desribes how and what data to retrieve. You''ll have to actually query the data first, e.g. by using the ToList extension method.

GridView1.DataSource = u.GetUsers.ToList()


尝试以下代码:

Try following code :

Public Function GetUsers() As List(Of User)
     Return context.Application_User.ToList()
End Function




-萨加尔·索兰基(Sagar Solanki)




-Sagar Solanki


这篇关于EntiryFramwork CodeFirst与GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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