问题描述
我是实体框架的新手,发现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
推荐答案
GridView1.DataSource = u.GetUsers.ToList()
Public Function GetUsers() As List(Of User)
Return context.Application_User.ToList()
End Function
-萨加尔·索兰基(Sagar Solanki)
-Sagar Solanki
这篇关于EntiryFramwork CodeFirst与GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!