我用c#,automapper和postgreSQL ef核心做了一个小例子。 我的实体是客户,产品,销售,销售产品。
我已经制作了客户和产品的映射,但我不知道如何映射销售和产品之间的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; }
}
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 Clients Client { get; set; }
public float TotalPrice { get; set; }
public DateTime? Date { get; set; }
public List<Products> Product { get; set; }
}
SalesProducts Model:
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();
}
}
有人知道我该如何正确映射?
答案 0 :(得分:0)
我尝试制作映射,但总是给出错误 未映射的产品:
您已经为如何映射两个SalesProducts
类定义了一个地图,但是它们具有不同的属性,因此您需要定义您希望如何映射这些属性。
CreateMap<DataModels.SalesProducts, Models.SalesProducts>();
具体来说,您的SalesProducts
之一拥有Product
和Sale
属性,另一个属性没有。您可以将其标记为忽略,否则您希望如何从没有这些属性的类创建它?
您也没有从DataModels.Product
到Models.Product
的映射...这可能是您的错误。