如何通过XML输入参数强制Entity Framework与存储过程一起使用?数据通过Sql连接完美地更新,但我只需要使用实体框架
我尝试过ExecuteFunction和ExecuteStoreQuery都给出了如下所示的不同错误
两个语句都给出错误
return base.ExecuteFunction<int>("pr_UpdateSupplierSubmittedExportdata_1", tblExportDataParameter, batchIDParameter, userNameParameter);
ex = {"The stored procedure or function 'pr_UpdateSupplierSubmittedExportdata_1' does not have a return type. ExecuteFunction only supports stored procedures and functions that have a return type. "}
return base.ExecuteStoreQuery<int>("Exec pr_UpdateSupplierSubmittedExportdata_1 @tblExportData, @BatchID, @UserName ", tblExportData, batchID, userName);
Must declare the scalar variable "@tblExportData".
var getColumn = (from sp in (objLCSEntities.pr_UpdateSupplierSubmittedExportdata_1(xmlString1, Convert.ToInt32(sExportID), strUserFullName))
select sp).First().ToString();
使用的存储过程:
CREATE PROCEDURE [UpdateSupplierSubmittedExportdata_1]
(
@tblExportData XML,
@BatchID INT,
@UserName varchar(200)
)
AS
BEGIN
SET NOCOUNT ON;
--UPDATE EXISTING RECORDS
-- import xml into temp table
-- Creating Temporary Table
CREATE TABLE #TEMP_TABLES
(
Updated_Accepted_Shipment_Date_EX_Works nvarchar(max),
Updated_Invoice_Number nvarchar(max),
Updated_Invoice_Date nvarchar(max),
Updated_Handover_Date nvarchar(max),
Updated_Revised_Delivery_Date_Ex_Works nvarchar(max),
Updated_PO_Line_Progress nvarchar(max),
Updated_Mode_of_Shipment nvarchar(max),
Updated_Supplier_Remarks nvarchar(max),
Updated_GSM_Remarks nvarchar(max),
Updated_Weight int,
Updated_Inspection_Offer_Date nvarchar(max),
Updated_Development nvarchar(max),
Updated_Transit_Qty int,
Order_x0020_ID int
);
-- insert into @temp
--select T.c.value('@CustomerID', 'nchar(5)'),
-- T.c.value('@ContactName', 'nvarchar(40)')
--from @XML.nodes('/ROOT/Customer') AS T(c)
INSERT INTO
#TEMP_TABLES
SELECT
(x.c.value('Updated_Accepted_Shipment_Date_EX_Works[1]', 'nvarchar(max)')),--as Updated_Accepted_Shipment_Date_EX_Works,
x.c.value('Updated_Invoice_Number[1]', 'nvarchar(max)'),-- as Updated_Invoice_Number,
x.c.value('Updated_Invoice_Date[1]', 'nvarchar(max)'),--as Updated_Invoice_Date,
x.c.value('Updated_Handover_Date[1]', 'nvarchar(max)'),-- as Updated_Handover_Date,
x.c.value('Updated_Revised_Delivery_Date_Ex_Works[1]', 'nvarchar(max)'),--as Updated_Revised_Delivery_Date_Ex_Works,
x.c.value('Updated_PO_Line_Progress[1]', 'nvarchar(max)'),--as Updated_PO_Line_Progress,
x.c.value('Updated_Mode_of_Shipment[1]', 'nvarchar(max)'),--as Updated_Mode_of_Shipment,
x.c.value('Updated_Supplier_Remarks[1]', 'nvarchar(max)'),--as Updated_Supplier_Remarks,
x.c.value('Updated_GSM_Remarks[1]', 'nvarchar(max)'),--as Updated_GSM_Remarks,
x.c.value('Updated_Weight[1]', 'int'),--as Updated_Weight,
x.c.value('Updated_Inspection_Offer_Date[1]', 'nvarchar(max)'),--as Updated_Inspection_Offer_Date,
x.c.value('Updated_Development[1]', 'nvarchar(max)'), --as Updated_Development
x.c.value('Updated_Transit_Qty[1]', 'int'), --as Updated_Development
x.c.value('Order_x0020_ID[1]', 'int') --as Updated_Development
FROM @tblExportData.nodes('/DocumentElement/MyTableName') as x(c)
--select * from #TEMP_TABLE232
--UPDATE EXISTING RECORDS
UPDATE dbo.tblExportOrderdata
SET Original_Ship_Dt = convert(datetime,c2.[Updated_Accepted_Shipment_Date_EX_Works])
,In_Transit_Qty = c2.[Updated_Transit_Qty]
,Invoice_No=c2.[Updated_Invoice_Number]
,Invoice_Date=c2.[Updated_Invoice_Date]
,Handover_Dt=c2.[Updated_Handover_Date]
,Rev_Delivery_Dt=c2.[Updated_Revised_Delivery_Date_Ex_Works]
,PO_line_progress=c2.[Updated_PO_Line_Progress]
,Mode_Of_Shipment=c2.[Updated_Mode_of_Shipment]
--,GSM_Buyer_Remark=c2.[Updated_GSM Remarks]
,Supplier_Remark=c2.[Updated_Supplier_Remarks]
,Inspection_Offer_Date=c2.[Updated_Inspection_Offer_Date]
,weight=c2.[Updated_Weight]
,Development=c2.[Updated_Development]
--,Status='Updated by Supplier'
FROM dbo.tblExportOrderdata c1
INNER JOIN #TEMP_TABLES c2
ON c1.Order_Id = c2.Order_x0020_ID and c1.Batch_ID=@BatchID
DROP table #TEMP_TABLES
IF EXISTS
(select 1 from [LCS_QA].[dbo].[tblExportInbox]
where Batch_No=@BatchID AND Status='Submitted To Supplier' )
--and Fls_Site_Name IN (SELECT * FROM DBO.CSVTOTABLE(@FlsSiteName))
--and Country IN (SELECT * FROM DBO.CSVTOTABLE(@Country))) --
BEGIN
UPDATE [LCS_QA].[dbo].[tblExportInbox] SET Status='Updated By Supplier' , Updated_Date=getdate() , Updated_By=@UserName
WHERE Batch_No=@BatchID --AND Status='Submitted To Supplier'
END
DECLARE @ResultValue int
SET @ResultValue=-1
select @ResultValue
END
答案 0 :(得分:0)
EF是在ADO.NET之上构建的,您始终可以从DbContext访问基础DbConnection来直接使用ADO.NET。
对于EF6,模式为:
using (var db = new MyDb())
{
var con = (SqlConnection) db.Database.Connection;
con.Open();
var cmd = con.CreateCommand();
//...
}
有关EF核心:
using (var db = new Db())
{
db.Database.OpenConnection();
var con = (SqlConnection)db.Database.GetDbConnection();
var cmd = con.CreateCommand();
//...
}