返回方法中存在实体框架编译错误

本文关键字:框架 编译 错误 实体 存在 方法 返回 | 更新日期: 2023-09-27 17:58:24

我将一个数据库导入到我的visual studio项目中。这是我的main.cs文件中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFramework
{
    class Program
    {
        static void Main(string[] args)
        {
            NorthwindEntities db = new NorthwindEntities();
            foreach (var customer in db.Customers)
            {
                Console.WriteLine(customer.ContactName);
            }
            db.Dispose();
        }
    }
}

问题是我得到了这个错误:

错误CS1502:的最佳重载方法匹配'System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction(字符串,params System.Data.Entity.Core.Objects.ObjectParameter[])"具有一些无效参数

当我双击它时,我会自动转到这种方法:

public virtual ObjectResult<CustOrderHist_Result> CustOrderHist(string customerID)
{
    var customerIDParameter = customerID != null ?
            new ObjectParameter("CustomerID", customerID) :
            new ObjectParameter("CustomerID", typeof(string));
//The error shows that the problem is in the return
                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<CustOrderHist_Result>("CustOrderHist", customerIDParameter);
            }

这表明问题出在方法的返回中。这个错误发生在任何写这样的return的地方。我还没有写这些函数。实体框架已经自动创建了它们。你对问题可能出在哪里有什么建议吗?

返回方法中存在实体框架编译错误

一些调查方法:

  • 您确定DB中的customerID是字符串格式的吗?是不是
    不是增量int
  • 您使用的是SQL Server还是其他数据库

自动生成的文件。Designer.cs非常适合简单的存储过程,因为它可以检索不复杂的对象,所以我认为您的SP没有正确导入edmx。检查SP方法是否是正确的I/O类型,也检查您是否使用另一个数据库作为SQL Server来配置好的OleDB。

请删除引用

using System.Data.Objects;

并添加

using System.Data.Entity.Core.Objects;

在上下文类中。