2017-03-08 67 views
1

我的ASP.Net MVC應用程序需要執行一個存儲過程,以檢查基於一組參數的重複工作訂單。存儲過程龐大而複雜,因此將其轉換爲Linq to SQL查詢是不可能的。此外,我們不使用實體數據模型(EDMX)文件;我們只有領域模型,視圖模型和數據上下文。存儲過程具有以下特徵:實體框架6從存儲過程中獲得完整的返回值

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 }); 

,並得到一個例外,當我運行此類似:

No mapping exists from object type <>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]] to a known managed provider native type.

我究竟做錯了什麼?


更新:當我將所有傳遞的參數更改爲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遏制。

+0

有你試過這種方式? 'IEnumerable duplicateCheckResults = db.Database.SqlQuery (「spGetDupWOs」,new DuplicateProjects(){ProjectName = ProjectName,City = City,State = propal.State,ProjectNumber = ProjectNumber,County = County,Owner = Owner ,BID_QuoteRecipients = QuoteRecipients,ProjectID = -1});'注意'new DuplicateProjects()'而不是匿名類型。 –

+0

我會試一試。 –

+0

美中不足的是DuplicateProjects是輸出的模型,而不是輸入。我現在有點慢。你認爲我應該把輸入變量變成SQL參數嗎? –

回答

2

此外參數SqlQuery<T>查詢字符串後構成params數組,該方法將使用回填查詢字符串編號參數參數,就像String.Format一樣。由於您傳遞的是匿名對象,因此該方法試圖將其視爲單個參數,並且無法將其轉換爲字符串。所以,是的,你要麼需要使用SqlParameter手動添加params中,改變您的呼叫處理它與該方法兼容的方式:

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 
); 

自由間距增加了更好的可讀性

+0

克里斯,我重構了一些代碼。你可以在UPDATE 2下面看到它。我越來越近了。 –

+0

修復了代碼。我遇到了一個字符串數組問題,但那是因爲我一直抓着錯誤的變量名。 –

相關問題