P.S реализую функцию добавления в корзину ASP.NET Core
Ошибка здесь
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WA12.DB;
namespace WA12.Models
{
public class ShopCart
{
private readonly AppDBContent appDBContent;
public ShopCart(AppDBContent appDBContent)
{
this.appDBContent = appDBContent;
}
public string ShopCartId { get; set; }
public List<ShopCartItem> listShopItems { get; set; }
public static ShopCart GetCart(IServiceProvider services)
{
ISession session = services.GetRequiredService<IHttpContextAccessor>()?.HttpContext.Session;
var context = services.GetService<AppDBContent>();
string shopCartId = session.GetString("CartId") ?? Guid.NewGuid().ToString();
session.SetString("CartId", shopCartId);
return new ShopCart(context) { ShopCartId = shopCartId };
}
public void AddToCart(Realty realty, int amount)
{
appDBContent.ShopCartItem.Add(new ShopCartItem
{
ShopCartId = ShopCartId,
realty = realty
});
appDBContent.SaveChanges();
}
}
}
public class ShopCartItem
{
public int id { get; set; }
public Realty realty { get; set; }
public int price { get; set; }
public string ShopCartId { get; set; }
}
public class AppDBContent : DbContext
{
public AppDBContent(DbContextOptions<AppDBContent> options) : base(options)
{
}
public DbSet<Realty> Realty { get; set; }
public DbSet<Category> Category { get; set; }
public DbSet<ShopCartItem> CartItem { get; set; }
}