实体框架6从存储过程获取完整的返回值

我的ASP.Net MVC应用程序需要执行一个存储过程,以便根据一组参数检查重复的工作单。 存储过程庞大而复杂,因此将其转换为Linq to SQL查询是不可能的。 此外,我们不使用实体数据模型(EDMX)文件; 我们只有领域模型,视图模型和数据上下文。 存储过程具有以下签名:

CREATE PROCEDURE [dbo].[spGetDupWOs]
    -- Add the parameters for the stored procedure here
    @ProjectName VARCHAR(200) = '""',
    @City VARCHAR(100) = '',
    @State VARCHAR(100) = '',
    @ProjectNumber VARCHAR(50) = '',
    @County VARCHAR(50) = '',
    @Customer VARCHAR(400) = '',
    @QuoteRecipients VARCHAR(400) = NULL,
    @ProjectID BIGINT = NULL
    ...

    SELECT DuplicateID, ProjectNameMatchRank, ProjectNameMatch, ProjectName ...

我试图在我的控制器中调用它,如:

IEnumerable<DuplicateProjects> duplicateCheckResults = db.Database.SqlQuery< DuplicateProjects>("spGetDupWOs", new { ProjectName = ProjectName, City = City, State = propal.State, ProjectNumber = ProjectNumber, County = County, Owner = Owner, BID_QuoteRecipients = QuoteRecipients, ProjectID = -1 });

当我像这样运行时得到一个异常:

没有映射存在对象类型<> f__AnonymousType6`8 [[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral ,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089], [System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.String,mscorlib ,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]转换为已知的托管提供程序本机类型。

我究竟做错了什么?


更新:当我将所有传递的参数更改为ObjectParamter时,我仍然收到相同的错误消息。


更新2:

我重构了一下代码。 这是它现在的样子:

            List<SqlParameter> spl = new List<SqlParameter>();

            var ProjectNameParameter = new SqlParameter("ProjectName", project.ProjectName);
            spl.Add(ProjectNameParameter);
            var CityParameter = new SqlParameter("City", project.City);
            spl.Add(CityParameter);
            var StateParameter = new SqlParameter("State", project.State);
            spl.Add(StateParameter);
            var ProposalNumberParameter = new SqlParameter("ProjectNumber", project.ProjectNumber);
            spl.Add(ProposalNumberParameter);
            var CountyParameter = new SqlParameter("County", project.County);
            spl.Add(CountyParameter);
            var OwnerParameter = new SqlParameter("Owner", project.Owner);
            spl.Add(OwnerParameter);
            var BidRecipientParameter = QuoteRecipients != null ?
                new SqlParameter("QuoteRecipients", QuoteRecipients) :
                new SqlParameter("QuoteRecipients", "");
            spl.Add(BidRecipientParameter);
            var ProjectIDParameter = new SqlParameter("ProjectID", typeof(long));
            spl.Add(ProposalIDParameter);

            IEnumerable<DuplicateProposals> duplicateCheckResults = db.Database.SqlQuery<DuplicateProposals>("spGetDupWOs ProjectName = {0} City = {1}, State = {2}, ProjectNumber = {3}, County = {4}, Owner = {5}, QuoteRecipients = {6}, ProjectID = {7}",
                spl.ToArray());

现在我得到错误:

SqlParameter已被另一个SqlParameterCollection包含。


在查询字符串构成一个params数组之后, SqlQuery<T>更多参数将被用于使用带有编号参数参数的查询字符串来回填,就像String.Format一样。 由于您传递的是匿名对象,因此该方法试图将其视为单个参数,并且无法将其转换为字符串。 所以,是的,你需要使用SqlParameter来手动添加参数或者改变你的调用来以与这个方法兼容的方式来处理它:

db.Database.SqlQuery< DuplicateProjects>(
    "spGetDupWOs @ProjectName = {0}, @City = {1}, @State = {2}, @ProjectNumber = {3}, @Country = {4}, @Owner = {5}, @BID_QuoteRecipients = {6}, @ProjectID = {7}",
    ProjectName,
    City,
    propal.State,
    ProjectNumber,
    County,
    Owner,
    QuoteRecipients,
    -1
);

增加了自由间距以提高可读性

链接地址: http://www.djcxy.com/p/52401.html

上一篇: Entity Framework 6 get complext return value from a stored procedure

下一篇: How to get name of method and class from where trace logs are sent in .net