我在 SQL Server 存盤程序中有一個這樣的查詢:
UPDATE StudentTable
SET isUpdate = 1, updateDate = GETDATE()
我希望它在 C# 中的 Linq-to-entities 中等效,以便GETDATE()使用。我不想使用本地客戶端時間之類的東西。我希望 SQL Server 時間是要存盤在updateDate列中的參考
uj5u.com熱心網友回復:
這對你有幫助嗎?
context.Students.Select (c => DateTime.Now);
等于:
SELECT GETDATE() AS 'Example'
FROM [dbo].[StudentTable]
來源
uj5u.com熱心網友回復:
這兩個選項可以幫助您實作目標:
在物體框架中使用原始 sql。
呼叫帶引數的存盤程序。
讓我們看看一些代碼:
// your input from a secure source
int input = 1;
// To add where and more lines you can use concatenation or string builder
string sql = $"update StudentTable set isUpdate={input},
updateDate=GETDATE()";
await db.Database.ExecuteSqlRawAsync(sql);
注意:出于安全原因,您可以使用引數代替字串插值。
這是一個帶有存盤程序和引數的示例。
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
//...
int input = 1;
long id = 10;
db.Database.ExecuteSqlRaw("exec [schema].[myCustomSP] @isUpdate, @id",
new SqlParameter("isUpdate", input),
new SqlParameter("id", id));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/401753.html
標籤:C# sql-server 实体框架 链接到实体
上一篇:強制轉換為類時JsonConvert.DeserializeObject回傳null
下一篇:可能有多個引數的動作引數
