NPoco一对多和多对多
NPoco One-To-Many and Many-To-Many
我很难将一对多和多对多 SQL 关系映射到我的 pocos 中的列表。我已经尝试了所有方式的 Fetch 和 Query 以及属性,但我没有正确映射 pocos。这是 类 和 SQL 的简化版本:
Pocos:
[NPoco.TableName("Product")]
[NPoco.PrimaryKey("ProductId")]
public class Product
{
public int ProductId { get; set; }
public List<Category> Categories { get; set; }
public string Name { get; set; }
public List<ProductVariant> ProductVariants { get; set; }
}
[NPoco.TableName("Category")]
[NPoco.PrimaryKey("CategoryId")]
public class Category : ModifiedDomainObject
{
public int CategoryId { get; set; }
public string Name { get; set; }
}
[NPoco.TableName("ProductVariant")]
[NPoco.PrimaryKey("ProductVariantId")]
public class ProductVariant : ModifiedDomainObject
{
public int ProductVariantId { get; set; }
public string Name { get; set; }
}
SQL查询:
SELECT[Product].[ProductId],
[Product].[PublicId],
[Product].[Name],
[Category].[CategoryId],
[Category].[Name],
[ProductVariant]
[ProductVariantId],
[ProductVariant].[ProductId],
[ProductVariant].[Name],
FROM[Product]
JOIN[ProductCategory] on[ProductCategory].[ProductId] = [ProductCategory].[ProductId]
JOIN[Category] ON[ProductCategory].[CategoryId] = [Category].[CategoryId]
LEFT OUTER JOIN[ProductVariant] ON[Product].[ProductId] = [ProductVariant].[ProductId]
WHERE[Product].[ProductId] = 1
ORDER BY[Product].[ProductId],
[Category].[CategoryId],
[ProductVariant].[ProductVariantId];
所以,Product->ProductVariant是一对多的,ProductVariant table携带ProductId; Product->Category 是多对多的,带有一个带有 ProductId 和 CategoryId 的外部参照 table [ProductCategory]。我得到的最接近的是 ProductVariant 列表,其中填充了正确数量的对象,但值是从产品数据映射而来的。
我已经使用 PetaPoco 很长时间了,现在正尝试 "upgrade" 到 NPoco V3。在使用 PetaPoco 的地方,我会使用 Relators 进行映射;使用 NPoco,在线示例对我不起作用。
使用 NPoco 3,您只能映射 1 个一对多或多对多关系。
要使示例正常工作,必须存在的项目是产品 class 中的 [NPoco.PrimaryKey("ProductId")] 标签。
所以你这样做:
string sql = "sql with product-categorie relation";
List<Product> products = db.Fetch<Product>(x => x.Categories, sql);
或
string sql = "sql with product-productVariant relation";
List<Product> products = db.Fetch<Product>(x => x.ProductVariants, sql);
这将为您提供带有类别列表或 ProductVariants 列表的产品列表,但不会同时包含两者。
您可以使用第一个,获取包含类别的产品列表,然后:
foreach(Product aProduct in products)
{
string productVariantSQL = "SQL to retrieve productVariant for current product";
aProduct.ProductVariants = db.Fetch<ProductVariant>(productVariantSQL);
}
我很难将一对多和多对多 SQL 关系映射到我的 pocos 中的列表。我已经尝试了所有方式的 Fetch 和 Query 以及属性,但我没有正确映射 pocos。这是 类 和 SQL 的简化版本:
Pocos:
[NPoco.TableName("Product")]
[NPoco.PrimaryKey("ProductId")]
public class Product
{
public int ProductId { get; set; }
public List<Category> Categories { get; set; }
public string Name { get; set; }
public List<ProductVariant> ProductVariants { get; set; }
}
[NPoco.TableName("Category")]
[NPoco.PrimaryKey("CategoryId")]
public class Category : ModifiedDomainObject
{
public int CategoryId { get; set; }
public string Name { get; set; }
}
[NPoco.TableName("ProductVariant")]
[NPoco.PrimaryKey("ProductVariantId")]
public class ProductVariant : ModifiedDomainObject
{
public int ProductVariantId { get; set; }
public string Name { get; set; }
}
SQL查询:
SELECT[Product].[ProductId],
[Product].[PublicId],
[Product].[Name],
[Category].[CategoryId],
[Category].[Name],
[ProductVariant]
[ProductVariantId],
[ProductVariant].[ProductId],
[ProductVariant].[Name],
FROM[Product]
JOIN[ProductCategory] on[ProductCategory].[ProductId] = [ProductCategory].[ProductId]
JOIN[Category] ON[ProductCategory].[CategoryId] = [Category].[CategoryId]
LEFT OUTER JOIN[ProductVariant] ON[Product].[ProductId] = [ProductVariant].[ProductId]
WHERE[Product].[ProductId] = 1
ORDER BY[Product].[ProductId],
[Category].[CategoryId],
[ProductVariant].[ProductVariantId];
所以,Product->ProductVariant是一对多的,ProductVariant table携带ProductId; Product->Category 是多对多的,带有一个带有 ProductId 和 CategoryId 的外部参照 table [ProductCategory]。我得到的最接近的是 ProductVariant 列表,其中填充了正确数量的对象,但值是从产品数据映射而来的。
我已经使用 PetaPoco 很长时间了,现在正尝试 "upgrade" 到 NPoco V3。在使用 PetaPoco 的地方,我会使用 Relators 进行映射;使用 NPoco,在线示例对我不起作用。
使用 NPoco 3,您只能映射 1 个一对多或多对多关系。
要使示例正常工作,必须存在的项目是产品 class 中的 [NPoco.PrimaryKey("ProductId")] 标签。
所以你这样做:
string sql = "sql with product-categorie relation";
List<Product> products = db.Fetch<Product>(x => x.Categories, sql);
或
string sql = "sql with product-productVariant relation";
List<Product> products = db.Fetch<Product>(x => x.ProductVariants, sql);
这将为您提供带有类别列表或 ProductVariants 列表的产品列表,但不会同时包含两者。
您可以使用第一个,获取包含类别的产品列表,然后:
foreach(Product aProduct in products)
{
string productVariantSQL = "SQL to retrieve productVariant for current product";
aProduct.ProductVariants = db.Fetch<ProductVariant>(productVariantSQL);
}