diff --git a/CarCenter/CarCenterContracts/ViewModels/CarViewModel.cs b/CarCenter/CarCenterContracts/ViewModels/CarViewModel.cs index 206d695..f24ef40 100644 --- a/CarCenter/CarCenterContracts/ViewModels/CarViewModel.cs +++ b/CarCenter/CarCenterContracts/ViewModels/CarViewModel.cs @@ -15,7 +15,7 @@ namespace CarCenterContracts.ViewModels public int? FeatureId { get; set; } [DisplayName("Цена особенности")] public double FeaturePrice { get; set; } - public int? StorekeeperId { get; set; } + public int StorekeeperId { get; set; } [DisplayName("Имя работника")] public string StorekeeperName { get; set; } = string.Empty; [DisplayName("Марка")] diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs b/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs index 1a92d72..3ce6f7c 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Bundling.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -23,6 +24,8 @@ namespace CarCenterDatabaseImplement.Models public ToolKit ToolKit { get; set; } = ToolKit.Неизвестно; [Required] public double Price { get; set; } + [ForeignKey("BundlingId")] + public virtual List CarBundling { get; set; } = new(); public static Bundling? Create(BundlingBindingModel model) { if (model == null) diff --git a/CarCenter/CarCenterDatabaseImplement/Models/Car.cs b/CarCenter/CarCenterDatabaseImplement/Models/Car.cs index c764294..e6adc9b 100644 --- a/CarCenter/CarCenterDatabaseImplement/Models/Car.cs +++ b/CarCenter/CarCenterDatabaseImplement/Models/Car.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -15,7 +16,7 @@ namespace CarCenterDatabaseImplement.Models public class Car : ICarModel { public int Id { get; private set; } - public int? StorekeeperId { get; set; } + public int StorekeeperId { get; set; } public int? FeatureId { get; set; } [Required] public CarBrand CarBrand { get; set; } = CarBrand.Неизвестно; @@ -31,11 +32,25 @@ namespace CarCenterDatabaseImplement.Models public long VINnumber { get; set; } [Required] public int FeatureID { get; set; } - public Dictionary CarBundlings { get; set; } = new(); public virtual Storekeeper Storekeeper { get; set; } public virtual Feature Feature { get; set; } - public static Car? Create(CarBindingModel model) + private Dictionary? _carBundlings = null; + [ForeignKey("CarId")] + public virtual List Bundlings { get; set; } = new(); + [NotMapped] + public Dictionary CarBundlings + { + get + { + if (_carBundlings == null) + { + _carBundlings = Bundlings.ToDictionary(recPc => recPc.BundlingId, recPc => recPc.Bundling as IBundlingModel); + } + return _carBundlings; + } + } + public static Car? Create(CarCenterDatabase context, CarBindingModel model) { if (model == null) { @@ -53,6 +68,10 @@ namespace CarCenterDatabaseImplement.Models Price = model.Price, VINnumber = model.VINnumber, FeatureID = model.FeatureID, + Bundlings = model.CarBundlings.Select(x => new CarBundling + { + Bundling = context.Bundlings.First(y => y.Id == x.Key) + }).ToList() }; } public void Update(CarBindingModel model) diff --git a/CarCenter/CarCenterDatabaseImplement/Models/CarBundling.cs b/CarCenter/CarCenterDatabaseImplement/Models/CarBundling.cs new file mode 100644 index 0000000..c0b8ea7 --- /dev/null +++ b/CarCenter/CarCenterDatabaseImplement/Models/CarBundling.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarCenterDatabaseImplement.Models +{ + public class CarBundling + { + public int Id { get; set; } + [Required] + public int CarId { get; set; } + [Required] + public int BundlingId { get; set; } + [Required] + public virtual Car Car { get; set; } = new(); + public virtual Bundling Bundling { get; set; } = new(); + } +}