ADO.Net 到 ASP DropDownList 的数据绑定错误

ADO.Net Data Binding Error to ASP DropDownList

错误信息:

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().

VS2012 中有什么变化吗?我以前使用过同样的策略吗?

检索数据的数据层代码:

namespace DataLibrary
{
    public class DLgetMasterData
    {
        POLLINATORSEntities db = new POLLINATORSEntities();

        public IEnumerable<string> getSoil()
        {
            var soilList = from SOTYPE in db.SOTYPEs
                           orderby SOTYPE.SOTYPE_DESC ascending
                           select SOTYPE.SOTYPE_DESC;
            return soilList;
        }

UI 中页面后面的代码:

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                BLgetMasterData obj = new BLgetMasterData();

                var soilList = obj.getSoil();
                Soil.DataSource = soilList;
                Soil.DataBind();
                Soil.Items.Insert(0, "Any");
                Soil.SelectedValue = "Any";

您不能绑定到一个查询,通常是一些 IQueryable(至少现在不能,不确定过去是否可以),所以您需要绑定到一些本地集合。

var soilList = from SOTYPE in db.SOTYPEs
                       orderby SOTYPE.SOTYPE_DESC ascending
                       select SOTYPE.SOTYPE_DESC;

soilList 表示将发送到数据库的查询,因此如果您在末尾添加 ToList(),将为您的查询创建一个本地结果集合,您将能够绑定到那个。

参见:Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported