[英]Initialize EF Core DbContext connection with DbConnection
I am migrating EF6 .net fwk application to Entity Framework Core 3.0.我正在将 EF6 .net fwk 应用程序迁移到 Entity Framework Core 3.0。 however, in my old EF6 project i had an option to initialize DbContext using a DbConnection Object, was used for implementing unit of work and use same transaction without changing the connection object.
但是,在我的旧 EF6 项目中,我可以选择使用 DbConnection 对象初始化 DbContext,用于实现工作单元并使用相同的事务而不更改连接对象。
public MyDataContext(IDbConnection connection)
: base((DbConnection) connection, false)
{
}
however, in EF core i have only one option to initialize the context that with a DbContextOptions, that takes a connection string.但是,在 EF 核心中,我只有一个选项来初始化带有 DbContextOptions 的上下文,它需要一个连接字符串。 and this will initialize new connection , and I wont be able to work with transaction.
这将初始化新连接,我将无法处理事务。
OLD EF code旧 EF 代码
var pipeline = endpointConfiguration.Pipeline;
pipeline.Register(new UnitOfWorkSetupBehavior(storageSession =>
{
var dbConnection = storageSession.SqlPersistenceSession().Connection;
var context = ***new MyDataContext(dbConnection);***
context.Database.UseTransaction(storageSession.SqlPersistenceSession().Transaction);
storageSession.SqlPersistenceSession().OnSaveChanges(x => context.SaveChangesAsync());
return context;
}), "Setting up unit of work");
Whats the alternative for the constructor with connection object, or what really changed in the EF core DBcontext not to take the connection ?具有连接对象的构造函数的替代方法是什么,或者在 EF 核心 DBcontext 中真正改变了什么以不接受连接?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.