我的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遏制。
有你試過這種方式? '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()'而不是匿名類型。 –
我會試一試。 –
美中不足的是DuplicateProjects是輸出的模型,而不是輸入。我現在有點慢。你認爲我應該把輸入變量變成SQL參數嗎? –