Я делаю небольшой пример с c #, automapper и postgreSQL ef core.Мои организации - это клиенты, продукты, продажи, sales_products.
Я уже сделал сопоставление клиентов и продуктов, но не знаю, как сопоставить отношения n-n между продажами и продуктами.
Вот мой код:
Субъект продаж:
public partial class Sales
{
public Sales()
{
SalesProducts = new HashSet<SalesProducts>();
}
public int SaleId { get; set; }
public int? ClientId { get; set; }
public float TotalPrice { get; set; }
public DateTime? Date { get; set; }
public Clients Client { get; set; }
public ICollection<SalesProducts> SalesProducts { get; set; }
}
Сущность продуктов:
public partial class Products
{
public Products()
{
SalesProducts = new HashSet<SalesProducts>();
}
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public float Price { get; set; }
public int Quant { get; set; }
public ICollection<SalesProducts> SalesProducts { get; set; }
}
Субъект продаж:
public partial class SalesProducts
{
public int ProductId { get; set; }
public int SaleId { get; set; }
public int? Quantity { get; set; }
public Products Product { get; set; }
public Sales Sale { get; set; }
}
Модель продуктов:
public class Products
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public float Price { get; set; }
public int Quant { get; set; }
}
Модель продаж:
public class Sales
{
public int SaleId { get; set; }
public Clients Client { get; set; }
public float TotalPrice { get; set; }
public DateTime? Date { get; set; }
public List<Products> Product { get; set; }
}
Модель SalesProducts:
public class SalesProducts
{
public int SaleId { get; set; }
public int ProductId { get; set; }
public int Quant { get; set; }
}
Я пытался создать сопоставление, но оно всегда выдавало ошибку о несопоставленном продукте:
public class SalesProfiles : Profile
{
public SalesProfiles()
{
CreateMap<DataModels.SalesProducts, Models.SalesProducts>();
CreateMap<DataModels.Sales, Models.SalesProducts>()
.ForMember(dest => dest.SaleId, opt => opt.MapFrom(src => src.SaleId))
.ReverseMap();
CreateMap<DataModels.Products, Models.SalesProducts>()
.ForMember(dest => dest.ProductId, opt => opt.MapFrom(src => src.ProductId))
.ReverseMap();
CreateMap<DataModels.Sales, Models.Sales>()
.ForMember(dest => dest.SaleId, opt => opt.MapFrom(src => src.SaleId))
.ForMember(dest => dest.Client, opt => opt.MapFrom(src => src.Client))
.ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.Date))
.ForMember(dest => dest.TotalPrice, opt => opt.MapFrom(src => src.TotalPrice))
.ForMember(dest => dest.Product, opt => opt.MapFrom(src => src.SalesProducts.Select(x => x.Product).ToList()));
}
}
Отображение продуктов:
public class ProductsProfiles : Profile
{
public ProductsProfiles()
{
CreateMap<Models.Products, BindingModels.Products>()
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.ProductName))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
.ForMember(dest => dest.Quant, opt => opt.MapFrom(src => src.Quant))
.ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price))
.ReverseMap();
CreateMap<Models.Products, DataModels.Products>()
.ForMember(dest => dest.ProductId, opt => opt.MapFrom(src => src.ProductId))
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.ProductName))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
.ForMember(dest => dest.Quant, opt => opt.MapFrom(src => src.Quant))
.ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price))
.ReverseMap();
CreateMap<BindingModels.Products, DataModels.Products>()
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.ProductName))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
.ForMember(dest => dest.Quant, opt => opt.MapFrom(src => src.Quant))
.ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price))
.ReverseMap();
CreateMap<DataModels.Products, Models.Products>()
.ForMember(dest => dest.ProductId, opt => opt.MapFrom(src => src.ProductId))
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.ProductName))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description))
.ForMember(dest => dest.Quant, opt => opt.MapFrom(src => src.Quant))
.ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price))
.ReverseMap();
}
}
Отображение клиентов:
public class ClientsProfiles : Profile
{
public ClientsProfiles()
{
CreateMap<Models.Clients, BindingModels.Clients>()
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
.ForMember(dest => dest.BirthDate, opt => opt.MapFrom(src => src.BirthDate))
.ReverseMap();
CreateMap<Models.Clients, DataModels.Clients>()
.ForMember(dest => dest.ClientId, opt => opt.MapFrom(src => src.ClientId))
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
.ForMember(dest => dest.BirthDate, opt => opt.MapFrom(src => src.BirthDate))
.ReverseMap();
CreateMap<BindingModels.Clients, DataModels.Clients>()
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
.ForMember(dest => dest.BirthDate, opt => opt.MapFrom(src => src.BirthDate))
.ReverseMap();
CreateMap<DataModels.Clients, Models.Clients>()
.ForMember(dest => dest.ClientId, opt => opt.MapFrom(src => src.ClientId))
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.FirstName))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.LastName))
.ForMember(dest => dest.BirthDate, opt => opt.MapFrom(src => src.BirthDate))
.ReverseMap();
}
}
Кто-нибудь знает, как я могу правильно сопоставить это?