diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs index d4e6e68..bb81317 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -28,5 +28,9 @@ namespace BlacksmithWorkshopDatabaseImplement public virtual DbSet ManufactureWorkPieces { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { set; get; } + + public virtual DbSet ShopManufactures { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs index 5724d6d..77e0003 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ShopStorage.cs @@ -17,7 +17,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { public List GetFullList() { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); return context.Shops .Include(x => x.Manufactures) .ThenInclude(x => x.Manufacture) @@ -33,7 +33,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements return new(); } - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); return context.Shops .Include(x => x.Manufactures) @@ -51,7 +51,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements return null; } - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); return context.Shops .Include(x => x.Manufactures) @@ -63,7 +63,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public ShopViewModel? Insert(ShopBindingModel model) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); var newShop = Shop.Create(context, model); if (newShop == null) @@ -79,7 +79,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public ShopViewModel? Update(ShopBindingModel model) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); using var transaction = context.Database.BeginTransaction(); try @@ -93,7 +93,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements shop.Update(model); context.SaveChanges(); - shop.UpdateIceCreams(context, model); + shop.UpdateManufactures(context, model); transaction.Commit(); return shop.GetViewModel; @@ -108,7 +108,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public ShopViewModel? Delete(ShopBindingModel model) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); var element = context.Shops .Include(x => x.Manufactures) @@ -127,15 +127,15 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public bool SellManufactures(IManufactureModel model, int count) { - using var context = new ManufactureShopDatabase(); + using var context = new BlacksmithWorkshopDatabase(); using var transaction = context.Database.BeginTransaction(); try { var shops = context.ShopManufactures - .Include(x => x.Shop) - .ToList() - .Where(rec => rec.ManufactureId == model.Id); + .Include(x => x.Shop) + .ToList() + .Where(rec => rec.ManufactureId == model.Id); if (shops == null) { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/ManufactureShopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/ManufactureShopDatabase.cs deleted file mode 100644 index e4e0942..0000000 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/ManufactureShopDatabase.cs +++ /dev/null @@ -1,34 +0,0 @@ -using BlacksmithWorkshopDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BlacksmithWorkshopDatabaseImplement -{ - public class ManufactureShopDatabase : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=ManufactureShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); - } - - public virtual DbSet WorkPieces { set; get; } - - public virtual DbSet Manufactures { set; get; } - - public virtual DbSet ManufactureWorkPieces { set; get; } - - public virtual DbSet Orders { set; get; } - - public virtual DbSet Shops { set; get; } - - public virtual DbSet ShopManufactures { get; set; } - } -} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs index 46f8fe7..a41c2d3 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Shop.cs @@ -44,7 +44,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [ForeignKey("ShopId")] public virtual List Manufactures { get; set; } = new(); - public static Shop? Create(ManufactureShopDatabase context, ShopBindingModel model) + public static Shop? Create(BlacksmithWorkshopDatabase context, ShopBindingModel model) { if (model == null) { @@ -89,20 +89,20 @@ namespace BlacksmithWorkshopDatabaseImplement.Models ShopManufactures = ShopManufactures }; - public void UpdateIceCreams(ManufactureShopDatabase context, ShopBindingModel model) + public void UpdateManufactures(BlacksmithWorkshopDatabase context, ShopBindingModel model) { - var shopIceCreams = context.ShopManufactures.Where(rec => rec.ShopId == model.Id).ToList(); + var shopManufactures = context.ShopManufactures.Where(rec => rec.ShopId == model.Id).ToList(); - if (shopIceCreams != null && shopIceCreams.Count > 0) + if (shopManufactures != null && shopManufactures.Count > 0) { // удалили те, которых нет в модели - context.ShopManufactures.RemoveRange(shopIceCreams.Where(rec => !model.ShopManufactures.ContainsKey(rec.ManufactureId))); + context.ShopManufactures.RemoveRange(shopManufactures.Where(rec => !model.ShopManufactures.ContainsKey(rec.ManufactureId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateIceCream in shopIceCreams) + foreach (var _shopManufactures in shopManufactures) { - updateIceCream.Count = model.ShopManufactures[updateIceCream.ManufactureId].Item2; - model.ShopManufactures.Remove(updateIceCream.ManufactureId); + _shopManufactures.Count = model.ShopManufactures[_shopManufactures.ManufactureId].Item2; + model.ShopManufactures.Remove(_shopManufactures.ManufactureId); } context.SaveChanges();