Applied access gap to dbcontext, Asp.net core web api 2.0 with first approach of core framework 2.0 database

I developed an asp.net core wep api 2.0 application with EntityFrameworkCore.SqlServer 2.0. It is developed using the first database approach. When you try to access objects using the dbcontext application, you will violate the mode. I cannot find a reason for the state of the application in a state of interruption. Please help solve this problem.

The following is the OnConfiguring method in the DBContext class.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(@"Server=(local);Database=VotingAppDB;User ID=sa;Password=123;"); } } 

Below is the code block used to access dbcontext objects in the controller

  // GET api/values [HttpGet] public IEnumerable<string> Get() { VotingAppDBContext context = new VotingAppDBContext(); var questions = context.Questions.ToList(); return new string[] { "value1", "value2" }; } 

Installed Packages

Application error

+5
source share
3 answers

1. Firstly, you should not create a context inside the controller, avoid using 'new' with dependencies, because it will make your code uncheckable, in my case when I use UnitOfWork, I inject it as an instance of IUnitOfWork, that is, really , the extension is MyConext, and you have to inject it into the StartUp class ... For this, I have a private method (for doing this in one private call) that looks like this:

  private void AddEntityFrameworkAndDbContext(IServiceCollection services) { services.AddEntityFrameworkSqlServer(); var migrationsAssemblyName = typeof(MyContext).GetTypeInfo().Assembly.GetName().Name; services.AddDbContext<MyContext>(options => { options.UseSqlServer( "MY CONNECTION STRING GOES HERE (BUT I RETREIVE IT FROM ANOTHER SERVICE)", sqlServerOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(migrationsAssemblyName); sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); }, ServiceLifetime.Scoped // Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request) ).AddUnitOfWork<MyContext>(); // This is because I'm also using EF Core Unit of work NuGet Package } 

I call this private method from ConfigureServices (IServiceCollection services), as I said, in the StartUp class

  // Add EF, and UoW AddEntityFrameworkAndDbContext(services); 

2.- Secondly ( but I would say that this is your real problem ) I would say that you missed the base. Setting (options); in your context, it should look like this:

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(@"Server=(local);Database=VotingAppDB;User ID=sa;Password=123;"); } base.OnConfiguring(optionsBuilder); } 

Also, please take a look at the answer I wrote a few weeks ago: How to configure EF6 Migrations with ASP.NET Core

Also, this UnitOfWork project is worth reading, see it here: https://github.com/arch/UnitOfWork

I hope this helps,

Juan

+1
source

Not sure if the problem still exists. Today I had the same problem. Fixed updating all nuget packages (in my case, Microsoft.Extensions.Configuration, etc.) to the latest version.

0
source

This happens when you independently refer to the primary key in your schema. Using the SQL Server database diagram, you can graphically display it. You must remove this link to solve this problem.

-1
source

Source: https://habr.com/ru/post/1271173/


All Articles