Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
858 views
in Technique[技术] by (71.8m points)

c# - Pass current transaction to DbCommand

I'm working on a project using ASP.NET Core 2.1 and EF Core 2.1. Although most of queries and commands use EF, some units needs to call stored procedures directly.

I can't use FromSql, because it needs results set based on entity models.

Let's assume we have these methods:

public Task CommandOne(DbContext context)
{
    Entity entity = new Entity
    {
        Name = "Name"
    };
    context.DbSet<Entity>().Add(entity);
    return context.SaveChangesAsync();
}

public async Task CommandTwo(DbContext context)
{
    DbCommand command = context.Database.GetDbConnection().CreateCommand();
    command.CommandText = "storedProcName";
    command.CommandType = System.Data.CommandType.StoredProcedure;

    using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false))
    {
        // read result sets
    }
}

If I call both commands in one transaction like this:

public async Task UnitOfWork(DbContext dbContext)
{
    using (var transaction = await dbContext.Database.BeginTransactionAsync())
    {
        await CommandOne(dbContext);
        await CommandTwo(dbContext);
    }
}

this exception will be happened:

BeginExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.

I have to mention, it's not as simple as command.Transaction = .... This requires DbTransaction which differs from the transaction EF uses.

I've stocked with this for a month! Is there any workaround for this? Thank you so much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have to mention, it's not as simple as command.Transaction = .... This requires DbTransaction which differs from the transaction EF uses.

Actually it is. All you need is a reference to Microsoft.EntityFrameworkCore.Relational assembly and add

using Microsoft.EntityFrameworkCore.Storage;

to get access to GetDbTransaction extension method:

if (context.Database.CurrentTransaction != null)
    command.Transaction = context.Database.CurrentTransaction.GetDbTransaction();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...