简体   繁体   English

创建模型时不能使用上下文。 EF-核心ASP.net Core2.2

[英]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.

相关问题 使用带有EF 6的ASP.NET Core控制台应用程序“创建模型时无法使用上下文” - “Context cannot be used while the model is being created” using ASP.NET Core Console Application With EF 6 EF中dbcontext的两个不同实例与“创建模型时无法使用上下文”冲突。 - Two different instances of my dbcontext in EF are colliding with “The context cannot be used while the model is being created.” 原因是:“在创建模型时不能使用上下文。”? - What is causing: “The context cannot be used while the model is being created.”? 如何在ASP.Net Core2.2中覆盖415响应 - How to Override 415 response in ASP.Net Core2.2 在创建模型时不能使用上下文。 dbContext正在其他地方使用? - The context cannot be used while the model is being created. dbContext is being used somewhere else? ASP.NET Identity 的“创建模型时无法使用上下文”异常 - "Context cannot be used while the model is being created" exception with ASP.NET Identity .Net Core2.2:如何在EF Codefirst方法中更改字符集 - .Net Core2.2 : how to change characterset in EF codefirst approach 创建模型时,不能使用数据上下文。 实体框架5 - The data context cannot be used while the model is being created. Entity Framework 5 使用HttpClient Alwyes Null ASP.net Core2.2将IFormFile发送到API - Send IFormFile To API using HttpClient Alwyes Null ASP.net Core2.2 LINQ和EF6:创建模型时无法使用上下文 - LINQ and EF6: The context cannot be used while the model is being created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM