SamusevWinApp/Product.cs

73 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
namespace samusev_42;
public partial class Product : IEditableObject
{
private Ispr2522SamusevOvLazarev2Context context;
public int IdProduct { get; set; }
public string Article { get; set; } = null!;
public int IdProductName { get; set; }
public string Unit { get; set; } = null!;
public decimal Price { get; set; }
public int IdSupplier { get; set; }
public int IdManufacturer { get; set; }
public int IdCategory { get; set; }
public int Discount { get; set; }
public int Count { get; set; }
public string Description { get; set; } = null!;
public string Photo { get; set; } = null!;
public virtual Category IdCategoryNavigation { get; set; } = null!;
public virtual Manufacturer IdManufacturerNavigation { get; set; } = null!;
public virtual ProductName IdProductNameNavigation { get; set; } = null!;
public virtual Supplier IdSupplierNavigation { get; set; } = null!;
public virtual ICollection<OrdersProduct> OrdersProducts { get; set; } = new List<OrdersProduct>();
// Реализация валидации IEditableObject
public void BeginEdit()
{
if (context == null)
{
context = new Ispr2522SamusevOvLazarev2Context();
context.Products.Attach(this);
}
}
public void CancelEdit()
{
if (context != null)
{
var entry = context.Entry(this);
if (entry.State == EntityState.Modified)
{
entry.Reload();
}
}
}
public void EndEdit()
{
// ВАЛИДАЦИЯ
if (string.IsNullOrWhiteSpace(this.Article))
{
throw new ValidationException("Артикул не может быть пустым!");
}
if (this.Price < 0)
{
throw new ValidationException("Цена не может быть отрицательной!");
}
if (context != null)
{
context.SaveChanges();
}
}
}