通过 Entity Framework 使用 Generic Repository 和 Unit Of work 获取多个表数据
Get multiple tables data through Entity Framework with Generic Repository and Unit Of work
我正在开发 Web-API 项目并使用 Entity Framework 与通用存储库和工作单元。基本上我遵循 this 的教程。
这是我的 table 架构。
实体
public class ProductEntity
{
public int ProductId { get; set; }
public string ProductCode { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ProductImgName { get; set; }
public bool IsActive { get; set; }
public int PrimaryCatId { get; set; }
public int SecondaryCatId { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public System.DateTime CreateDate { get; set; }
public List<PrimaryProductEntity> objPrimaryProduct { get; set; }
public List<SecondaryProductEntity> objSecondaryProduct { get; set; }
}
public class PrimaryProductEntity
{
public int PrimaryCatId { get; set; }
public string PrimaryCatName { get; set; }
}
public class SecondaryProductEntity
{
public int SecondaryCatId { get; set; }
public string SecondaryCatName { get; set; }
public int PrimaryCatId { get; set; }
}
服务代码
public IEnumerable<BusinessEntities.ProductEntity> GetAllProducts()
{
var products = _unitOfWork.ProductRepository.GetAll().ToList();
var primaryProducts = _unitOfWork.PrimaryProductRepository.GetAll().ToList();
var secondaryProducts = _unitOfWork.SecondaryProductRepository.GetAll().ToList();
if (products.Any())
{
Mapper.CreateMap<tblProduct, ProductEntity>();
var proInfo = from P in products
join PP in primaryProducts on P.PrimaryCatId equals PP.PrimaryCatId
join SP in primaryProducts on P.SecondaryCatId equals SP.SecondaryCatId
select P;
var productsModel = Mapper.Map<List<tblProduct>, List<ProductEntity>>(proInfo);//getting error
return productsModel;
}
return null;
}
我知道我的实现是错误的,我不知道在代码中写什么来从多个 table 中获取数据。请帮助我。
所需数据
ProductID、ProductName、PrimaryCatName、SecondaryCatName、价格、数量
您的产品实体 class 不需要 List<PrimaryProductEntity>
和 List<SecondaryProductEntity>
。我想根据你的 class 图每个 Product
与一个 PrimaryProductEntity
和一个 SecondaryProductEntity
.
相关联
一旦您的模型 class 得到更正,您就可以访问导航的属性。我不太擅长按照您想要的方式编写查询。但我希望你能知道你应该做什么
我正在开发 Web-API 项目并使用 Entity Framework 与通用存储库和工作单元。基本上我遵循 this 的教程。
这是我的 table 架构。
实体
public class ProductEntity
{
public int ProductId { get; set; }
public string ProductCode { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ProductImgName { get; set; }
public bool IsActive { get; set; }
public int PrimaryCatId { get; set; }
public int SecondaryCatId { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public System.DateTime CreateDate { get; set; }
public List<PrimaryProductEntity> objPrimaryProduct { get; set; }
public List<SecondaryProductEntity> objSecondaryProduct { get; set; }
}
public class PrimaryProductEntity
{
public int PrimaryCatId { get; set; }
public string PrimaryCatName { get; set; }
}
public class SecondaryProductEntity
{
public int SecondaryCatId { get; set; }
public string SecondaryCatName { get; set; }
public int PrimaryCatId { get; set; }
}
服务代码
public IEnumerable<BusinessEntities.ProductEntity> GetAllProducts()
{
var products = _unitOfWork.ProductRepository.GetAll().ToList();
var primaryProducts = _unitOfWork.PrimaryProductRepository.GetAll().ToList();
var secondaryProducts = _unitOfWork.SecondaryProductRepository.GetAll().ToList();
if (products.Any())
{
Mapper.CreateMap<tblProduct, ProductEntity>();
var proInfo = from P in products
join PP in primaryProducts on P.PrimaryCatId equals PP.PrimaryCatId
join SP in primaryProducts on P.SecondaryCatId equals SP.SecondaryCatId
select P;
var productsModel = Mapper.Map<List<tblProduct>, List<ProductEntity>>(proInfo);//getting error
return productsModel;
}
return null;
}
我知道我的实现是错误的,我不知道在代码中写什么来从多个 table 中获取数据。请帮助我。
所需数据
ProductID、ProductName、PrimaryCatName、SecondaryCatName、价格、数量
您的产品实体 class 不需要 List<PrimaryProductEntity>
和 List<SecondaryProductEntity>
。我想根据你的 class 图每个 Product
与一个 PrimaryProductEntity
和一个 SecondaryProductEntity
.
一旦您的模型 class 得到更正,您就可以访问导航的属性。我不太擅长按照您想要的方式编写查询。但我希望你能知道你应该做什么