Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

EF Core - InvalidOperationException: No database provider has been configured for this DbContext

$
0
0

Hi all! I have this message after my application is starting and fetching data from db.

InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

  • Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
  • Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
  • Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
  • Microsoft.EntityFrameworkCore.DbContext.get_Model()
  • Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.get_EntityType()
  • Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.CheckState()
  • Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.get_EntityQueryable()
  • Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.System.Linq.IQueryable.get_Provider()
  • Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTracking<TEntity>(IQueryable<TEntity> source)
  • testweb.Startup+<>c+<<Configure>b__3_1>d.MoveNext() in Startup.cs
  • temp = bag.Account.AsNoTracking().First();

  • Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
  • Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
public class Startup
    {

        public Startup(IConfiguration configuration)
        {

            ConnectionString = Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString(configuration, "SqlServer");
        }
         public static string ConnectionString;
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<TotalForumContext>(options => options.UseSqlServer(ConnectionString));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    Account temp;
                     using(var bag = new TotalForumContext())        
                   temp = bag.Account.AsNoTracking().First();
                   await context.Response.WriteAsync(temp.Nick);
                });
            });
        }
    }
public partial class TotalForumContext : DbContext
    {
   public TotalForumContext()
   {}

        public TotalForumContext(DbContextOptions<TotalForumContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Account> Account { get; set; }

}
{"ConnectionStrings": {"SqlServer": "Server=localhost; Database=TotalForum; User Id=forumadminuser; Password=password"
  }
}
<ItemGroup><PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.3" /><PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" /><PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" /></ItemGroup>

What did happen? Why that doesn't work? Thank you.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>