locked
"An attempt was made to use the context while it is being configured" error RRS feed

  • Question

  • User283528319 posted

    Hi All,

    <g class="gr_ gr_26 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="26" data-gr-id="26">Obviously</g> I have <g class="gr_ gr_380 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="380" data-gr-id="380">problem</g> with my DBcontext setup because when I try to get data from 2 different <g class="gr_ gr_89 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="89" data-gr-id="89">table</g> with 2 different <g class="gr_ gr_228 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="228" data-gr-id="228">context</g> via 2 in succession jquery $.get method I got the error

    System.InvalidOperationException
    HResult=0x80131509
    Message=An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point. This can happen if a second operation is started <g class="gr_ gr_130 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="130" data-gr-id="130">on</g> this context before a previous operation completed. Any instance members are not guaranteed to be thread-safe.
    Source=Microsoft.EntityFrameworkCore

    Connection settings

         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseMySQL("server=localhost;port=3306;user=root;password=xxx;database=evraka");                
                }
            }

    startup

     services.Add(new ServiceDescriptor(typeof(EvrakaContext), new EvrakaContext(Configuration.GetConnectionString("EvrakaContext"))));

    usage 

    neither of these ways  <g class="gr_ gr_573 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="573" data-gr-id="573">are</g> working 

          public List<string> TumIllerListesi()
            {
                EvrakaContext context = HttpContext.RequestServices.GetService(typeof(EvrakaContext)) as EvrakaContext;
                           
                return context.Set<Iller>().Select(u => u.Il).ToList();
              
                
            }
        
        private readonly EvrakaContext _context;
    
            public NumuneKabulController(EvrakaContext context)
            {
                _context = context;
            }
    
    public  IList<string> GonderenListesiYukle()
            {
               
                return  _context.Set<Gonderenler>().Select(u => u.Gonderenmakam).ToList();
                //Debugger.Break();         
    
    
            }

    Monday, January 28, 2019 11:21 AM

All replies

  • User1120430333 posted

    I'll assume that this issue about the connectionstring has solved your problems that you addressed in the MVC forum?

    Monday, January 28, 2019 8:47 PM
  • User283528319 posted

    <g class="gr_ gr_77 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="77" data-gr-id="77">Yes</g> it seems so but couldn't be sure and wanted to be sure to get other ideas in here if exists.

    Tuesday, January 29, 2019 11:26 AM