Multi-async in Entity Framework 6?

前端 未结 3 1945
说谎
说谎 2020-11-28 07:54

This is my code:

var banner = context.Banners.ToListAsync()
var newsGroup = context.NewsGroups.ToListAsync()
await Task.WhenAll(banner, newsGroup);
         


        
3条回答
  •  离开以前
    2020-11-28 08:32

    If you use Unity for dependency injection with for example repository pattern you will get the following error using two or more contexts with create/update/delete:

    The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.

    This can be solved using PerRequestLifetimeManager. More info here:

    C# EF6 make multiple async calls to one context using Unity - Asp.Net Web Api

    container.RegisterType(new PerRequestLifetimeManager());
    container.RegisterType();
    container.RegisterType();
    

提交回复
热议问题