How to implement dependency injection of db context

asp.net-core-mvc c# dependency-injection entity-framework-core

Question

I'm trying to adapt this tutorial on how to make a shopping cart in asp.net core mvc but I'm having trouble with the db context. I get the following error:

Error CS7036 There is no argument given that corresponds to the required formal parameter 'options' of 'StoreContext.StoreContext(DbContextOptions)'

I've already looked at this question and implemented the solution but it still hasn't fixed my problem.

Here is my code:

    public class ShoppingCart
    {
        private readonly StoreContext db;

        public ShoppingCart(StoreContext context)
        {
            db = context;
        }

        string ShoppingCartId { get; set; }

        public const string CartessionKey = "CartId";

        public static ShoppingCart GetCart(HttpContext context)
        {
            var cart = new ShoppingCart();
            cart.ShoppingCartId = cart.GetCartId(context);
            return cart;
        }
}

If I change the code, as suggested, to the following:

    public static ShoppingCart GetCart(HttpContext context)
    {
        **var cart = new ShoppingCart(db);**
        cart.ShoppingCartId = cart.GetCartId(context);
        return cart;
    }

Then I get a new error:

System.NullReferenceException: Object reference not set to an instance of an object.

So, despite the corrections, it seems that it's still not injecting the db properly. Any guidance?

And here for the storecontext:

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

    public DbSet<Product> Product { get; set; }
    public DbSet<Supplier> Supplier { get; set; }
    public DbSet<Cart> Cart { get; set; }
    public DbSet<Order> Order { get; set; }
    public DbSet<OrderDetail> OrderDetail { get; set; }
}

Here's is the connection string in my startup:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddDbContext<StoreContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("StoreContext")));
    }
1
0
2/18/2019 12:19:24 AM

Accepted Answer

LakeViewContext expects a DbContextOptions<LakeViewContext> to be passed into its constructor. However, you are calling the constructor without providing anything:

private LakeViewContext db = new LakeViewContext();

To fix the issue, you can just plug into the Dependency Injection system that you've set up. To do this, change your controller as follows:

public class CoursesController : Controller
{
    private readonly LakeViewContext db;

    public CoursesController(LakeVieContext db)
    {
        this.db = db;
    }

    ...

The ASP.NET Core Dependency Injection system will provide you with a LakeViewContext in the constructor - Just use that.

8
9/23/2017 9:14:18 PM

Popular Answer

When observing at how the classes are generated via the database first approach, I was able to fix this error with an empty constructor.

public class MyDBContext : DbContext
{
    public MyDBContext()
    {
    }
//Rest of your code

}



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow