У меня есть эти объекты базы данных, которые я хочу отобразить:
Продукты
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 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; }
}
SalesProducts
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 int ClientId { get; set; }
public string ClientFirstName { get; set; }
public string ClientLastName { get; set; }
public float TotalPrice { get; set; }
public DateTime? Date { get; set; }
}
SalesDetails
public class SalesDetails
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
}
Все карты продаж:
public class SalesProfiles : Profile
{
public SalesProfiles()
{
CreateMap<DataModels.Sales, Models.Sales>()
.ForMember(dest => dest.SaleId, opt => opt.MapFrom(src => src.SaleId))
.ForMember(dest => dest.ClientId, opt => opt.MapFrom(src => src.Client.ClientId))
.ForMember(dest => dest.ClientFirstName, opt => opt.MapFrom(src => src.Client.FirstName))
.ForMember(dest => dest.ClientLastName, opt => opt.MapFrom(src => src.Client.LastName))
.ForMember(dest => dest.Date, opt => opt.MapFrom(src => src.Date))
.ForMember(dest => dest.TotalPrice, opt => opt.MapFrom(src => src.TotalPrice));
CreateMap<DataModels.Products, Models.SalesDetails>()
.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.Quantity, opt => opt.Ignore());
CreateMap<DataModels.SalesProducts, Models.SalesDetails>()
.ForMember(dest => dest.ProductId, opt => opt.MapFrom(src => src.Product.ProductId))
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.Product.ProductName))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Product.Description))
.ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quantity));
}
}
Отображение продуктов
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();
}
}
Это отношения между продажами и продуктами.Кто-нибудь знает, как я могу правильно сопоставить это с помощью автоматического преобразователя?
Я уже пытался сопоставить, но всегда говорит, что свойства продуктов не сопоставлены