简体   繁体   English

如何在asp.net core和ef7中的并行方法中使用注入的DbContext?

[英]How to use injected DbContext in parallel methods in asp.net core and ef7?

I have an Asp.net 5/Core 1 application with EF7. 我有一个EF7的Asp.net 5 / Core 1应用程序。 I register the DbContext in the DI container normally. 我通常在DI容器中注册DbContext。

services.AddEntityFramework().AddSqlServer()
  .AddDbContext<MyDbContext>(options => options.UseSqlServer(connection));

Here is a small sample showing what I'm trying to do. 这是一个小样本,展示了我正在尝试做的事情。

public class MyController : Controller
{
    MyDbContext _myDbContext;

    public MyController(MyDbContext myDbContext)
    {
        _myDbContext = myDbContext;
    }

    public IActionResult Index()
    {
        //Just start these and don't wait for them to finish
        //We don't care if they succeed or not
        Task.Run(() => DoSomeLogging1());
        Task.Run(() => DoSomeLogging2());

        return View();
    }

    private void DoSomeLogging1()
    {
        _myDbContext.Items.ToList();
    }

    private void DoSomeLogging2()
    {
        _myDbContext.Items.ToList();
    }
}

Both DoSomeLoggingX methods will use the MyDbContext instance that was injected to the controller. 两个DoSomeLoggingX方法都将使用注入控制器的MyDbContext实例。 Since the methods are run at the same time doing db queries at the same time, the other one will invariably fail with 由于这些方法同时在进行数据库查询的同时运行,因此另一个方法总是会失败

The connection was not closed. 连接没有关闭。 The connection's current state is connecting. 连接的当前状态是连接。

MyDbContext also uses DI to get some references so I can't use it directly even if I wanted. MyDbContext也使用DI来获取一些引用,所以即使我想要也不能直接使用它。

How can I run code in parallel and still be able to use my database through entity framework? 如何并行运行代码并仍然可以通过实体框架使用我的数据库?

I also faced with this problem and for now I use my temporary solution EF 7 (Core). 我也遇到了这个问题,现在我使用我的临时解决方案EF 7(Core)。 Create DBContext like AddTransient 像AddTransient一样创建DBContext

If someone can provide more elegant solution I will be appreciated. 如果有人能提供更优雅的解决方案,我将不胜感激。 Sometimes I really need to return instant result to user and in such cases await/async does not suit 有时我真的需要将即时结果返回给用户,在这种情况下,等待/异步不适合

Simple answer: you can't. 简单回答:你做不到。 EF is not made for parallel sql execution (and never will be, I guess). EF不是为并行sql执行而做的(我想也永远不会)。

Less-simple answer. 不太简单的答案。 Probably this is not the right way to approach your problem (@Tseng made a point in his comments), but if you need to do a logging stuff like that you can: 可能这不是解决问题的正确方法(@Tseng在他的评论中提出了一点意见),但是如果你需要做类似的日志记录,你可以:

  • make the two logging action sequential and share the same EF instance 使两个日志记录操作顺序并共享相同的EF实例
  • use plain old SQL with a SqlConnection object (which works perfectly with parallel processing) - and it's faster 使用普通的旧SQL和SqlConnection对象(它与并行处理完美配合) - 而且速度更快
  • use a logging framework like NLog which supports log-and-forget approach 使用像NLog这样的日志记录框架,它支持log-and-forget方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core 中使用 DbContext 注入并行 EF Core 查询 - Parallel EF Core queries with DbContext injection in ASP.NET Core ASP.NET vNext EF7 dbContext问题 - ASP.NET vNext EF7 dbContext issues 如何使用 EF Core 更新 ASP.NET 5 中的 dbcontext 脚手架? - How to update dbcontext scaffolding in ASP.NET 5 with EF Core? ASp.net身份如何为EF使用相同的dbcontext? - ASp.net Identity How to use the same dbcontext for EF? 在 ASP.NET Core 请求结束后使用(DI 注入)DbContext - Use (DI injected) DbContext after request ended in ASP.NET Core 覆盖 ASP.NET Core WebApplicationFactory 中的 EF Core DbContext - Override EF Core DbContext in ASP.NET Core WebApplicationFactory 在使用 EF Core 在 ASP.NET Core 中执行 CRUD 时,我应该单独使用 DbContext 还是使用 DbSet - Should I use DbContext alone or DbSet when performing CRUD in ASP.NET Core using EF Core 如何在 ASP.NET Core 5 中使用动态 DBContext 作为参数 - How to use dynamic DBContext as parameter in ASP.NET Core 5 如何在 ASP.NET Core 的多个身份中使用单个 DBContext? - How to use single DBContext in multiple identities in ASP.NET Core? 在ASP.NET核心中使用时,如何从另一个项目访问EF核心的DbContext? - How to I access the DbContext of EF core from another project when used in ASP.NET core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM