Исправление модели изделия, создание класса с таблицами

This commit is contained in:
prodigygirl 2023-02-26 22:01:55 +04:00
parent ac8a0e4602
commit 9cc2836618
3 changed files with 28 additions and 6 deletions

View File

@ -0,0 +1,26 @@
using FurnitureAssemblyDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyDatabaseImplement
{
public class FurnitureAssemblyDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=zhu\SQLEXPRESS;Initial Catalog=FurnitureAssemblyDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Furniture> Furnitures { set; get; }
public virtual DbSet<FurnitureComponent> FurnitureComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -20,8 +20,4 @@
<ProjectReference Include="..\FurnitureAssemblyDataModels\FurnitureAssemblyDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
</Project>

View File

@ -69,7 +69,7 @@ namespace FurnitureAssemblyDatabaseImplement.Models
var furnitureComponents = context.FurnitureComponents.Where(rec => rec.ProductId == model.Id).ToList();
if (furnitureComponents != null && furnitureComponents.Count > 0)
{ // удалили те, которых нет в модели
context.ProductComponents.RemoveRange(furnitureComponents.Where(rec
context.FurnitureComponents.RemoveRange(furnitureComponents.Where(rec
=> !model.FurnitureComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
@ -83,7 +83,7 @@ namespace FurnitureAssemblyDatabaseImplement.Models
var furniture = context.Furnitures.First(x => x.Id == Id);
foreach (var pc in model.FurnitureComponents)
{
context.ProductComponents.Add(new FurnitureComponent
context.FurnitureComponents.Add(new FurnitureComponent
{
Furniture = furniture,
Component = context.Components.First(x => x.Id == pc.Key),