score:13
You are using dependency injection. services.AddDbContext
is in charge of creating you an DbContext object.
There is no point in having that using block because, by doing that, you are instancing a new ApplicationDbContext that doesn't have a connection string.
Write your method like this:
public HeaderModel GetHeaderInformation(ApplicationDbContext context)
{
// the code inside your using block
}
and .Net will resolve the context via dependency injection.
Furthermore, a common practice is to have the DbContext as a private readonly atribute in your constructor class. So you may want do something like this:
public class MyConroller : Controller
{
private readonly MyDbContext _context;
public MyConroller(MyDbContext ctx)
{
_context = ctx;
}
}
and just use the context atribute in your methods.
score:0
You should remove your parameterless constructor, because it is probably the one that is being called when a new instance is created by the DI. I know that for some people this was the problem, I hope it helps.
Source: stackoverflow.com
Related Articles
- DotNet Core , No database provider has been configured for this DbContext
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to make this SQL query into DotNet Core 2.1 Linq
- What does this C# code with an "arrow" mean and how is it called?
- The operation cannot be completed because the DbContext has been disposed using MVC 4
- Is it possible to express this code in LINQ?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Best open source LINQ provider
- Does this LINQ code perform multiple lookups on the original data?
- Why doesn't this code compile in VS2010 with .NET 4.0?
- Why is this code with PredicateBuilder not working?
- Does this code really cause an "access to modified closure" problem?
- LINQ Source Code Available
- How does this linq code that splits a sequence work?
- multiple orderby in this linq code
- How can I combine this code into one or two LINQ queries?
- Difference between the returned values of two selector functions in this code
- .NET 4 Code Contracts: "requires unproven: source != null"
- Is there a way to speed up this code that finds data changes in two XML files?
- Can Someone Explain This MSDN Code To Me In English?
- How can I further simplify this piece of LINQ code
- Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)
- How to render this map-reduce javascript code to an equivalent LINQ Select-Aggregate?
- How to improve this Entity Framework Core query?
- LINQ approach to this code
- How can I modify this C# code so that Visual Studio recognizes that I'm not an idiot?
- How might I convert this SQL code to LINQ?
- creating Linq to sqlite dbml from DbLinq source code
- What would be a reasonably fast way to code this sql query in c#?
- Correct interpretation of SQL request by EF Core
- Returning a list of integers and using .contains()
- LINQ to Dataset Left Join
- Include Derived Models Related Class
- Linq to SQL & Logical partitioning (DAL, BLL)
- How to use GroupBy properly in LINQ?
- Create GroupBy Statements Dynamically
- c# wpf filter list In Statement
- How to defer execution of IEnumerable-returning function that begins a LINQ expression?
- Upgrade to 4.0 Framework, then downgraded, now can't find Linq namespace?
- Is there a way to set the default namespace to query from an XDocument?
- Is the order of an except and intersect operation always ignorable?
- How to combine two lists<Class> without duplicates in one of variables
- Why does LINQ with LazyLoading randomly NOT load children?
- How to Group a List of Objects by a Running Total?
- return biggest value from query
- how to use an expression in a select clause
- Linq to sql - get value from db function and not directly from the db field (while mapping properties)
- Reverse a dictionary from <Parent, IEnumerable<Child>> to <Child, IEnumerable<Parent>>
- ASP.NET - SQL - Retrieving 'contained' objects