[英]The context cannot be used while the model is being created. EF-Core ASP.net Core2.2
I have seen many posts talking about the problem but none of them fixed my problem 我看过很多关于这个问题的文章,但是都没有解决我的问题
the scenario DB Layer with API Controllers IDataRepository DataManagers API控制器IDataRepository DataManagers的场景数据库层
Code 码
Startup.cs Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationContext>(opts => opts.UseSqlServer(Configuration["ConnectionString:LawyerApplicationDB"]), ServiceLifetime.Transient);
services.AddSingleton(typeof(IDataRepository<Clients, long>), typeof(ClientManager));
services.AddSingleton(typeof(IDataRepository<Nationality, long>), typeof(NationalityManager));
services.AddMvc();
}
ApplicationContext 的ApplicationContext
public class ApplicationContext: DbContext
{
public ApplicationContext(DbContextOptions opts) : base(opts)
{
}
public DbSet<Clients> Clients { get; set; }
public DbSet<Nationality> Nationalities { get; set; }
}
The Manager where the error Appear 出现错误的Manager
public class NationalityManager : IDataRepository<Nationality, long>
{
private ApplicationContext ctx; //not static
public NationalityManager(ApplicationContext c)
{
ctx = c;
}
public Nationality Get(long id)
{
var nationality = ctx.Nationalities.FirstOrDefault(b => b.Id == id);
return nationality;
}
public IEnumerable<Nationality> GetAll()
{
var nationalities = ctx.Nationalities.ToList();
return nationalities;
}
the error appear first time and the grid does not show data if i refresh the page the data will show 该错误首次出现,并且如果我刷新数据将显示的页面,网格将不会显示数据
what i do wrong 我做错了
this is the tutorial i used Building An ASP.NET Core Application With Web API And Code First Development 这是我使用Web API和Code First开发构建ASP.NET Core应用程序的教程
Thank you for your help 谢谢您的帮助
You've fallen into a classic situation where you are keeping the context around too long. 您陷入了一种经典情况,即保持上下文的时间过长。
Because NationalityManager
is registered as a singleton it doesn't matter that your context was registered as transient. 因为NationalityManager
已注册为单例,所以您的上下文已注册为瞬态也没关系。 Something with a short lifetime injected into something with a long lifetime effectively means the short lifetime is extended by the lifetime of the longer lived thing. 生命周期短的事物有效地注入了生命周期较长的事物,这意味着生命周期较短的事物的寿命会延长。
You can either make your manager object shorter lived or you can inject a context factory into your manager. 您可以缩短经理对象的寿命,也可以将上下文工厂注入经理。 The context factory ensures your (what should be short lived) context is created when its needed. 上下文工厂确保在需要时创建您的(应该是短暂的)上下文。
When you have API calls coming in at the same time, they are attempting to use the non-thread safe context simultaneously. 当同时有API调用进入时,它们将尝试同时使用非线程安全上下文。 The first call is setting up the model, and then here comes another call wanting to use the model while its being setup . 第一个调用是建立模型,然后是另一个调用,它想在建立模型时使用模型。
Prior to EF Core, I've addressed this issue with the original EF designed for .NET Framework. 在使用EF Core之前,我已经使用为.NET Framework设计的原始EF 解决了这个问题 。 It might give you some more background. 它可能会为您提供更多背景知识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.