[英]How to add an existing stored procedure to dbcontext with Entity Framework Core
[英]How to create a DBContext as part of an existing transaction in Entity Framework Core
我有已經使用ADO.Net來訪問SQL Server的產品,並且我正在使用Entity Framework Core來提供一些新功能。 我遇到的問題是我必須在單個事務中完成一些操作,並且前幾個動作是通過ADO.Net實現的,我需要使用實體框架添加一個額外的動作,因此我需要將事務傳遞給DBContext 。
當我需要使用現有連接時,可以執行以下操作:
var dbc = new MyDBContext(optionsBuilder.UseSqlServer(connection).Options)
我在事務而不是連接中傳遞類似的內容嗎? 我應該以完全不同的方式來執行此操作嗎?
您可以執行類似的操作以使用現有的SqlTransaction創建DbContext。 這對我有用。 我在同一事務上同時使用ADO.NET和EF進行CRUD操作。
//create connection and transaction do usual ADO.NET stuff
SqlTransaction transaction = ... // existing transaction
var existingConnection = transaction.Connection;
var contextOwnsConnection = false;
using (var db = new MyDbContext(existingConnection, contextOwnsConnection))
{
db.Database.UseTransaction(transaction);
//do something with db
}
// optionally do usual ADO.NET stuff ....
transaction.Commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.