问题描述
我有一个场景,我在数据库中有3种类型的产品,所有产品都有自己独立的表格(例如 Product1
, Product2
和 Product3
)。几乎所有的产品表都具有相同的模式。我有要求在不同的表格中获取不同类型的产品。 我目前有3种方法来获取产品,每种产品类型有一种:
public List< Product1> GetProduct1Data(){
// ....
context.Product1.Where(..)。Tolist();
}
public List< Product2> GetProduct2Data(){
// ....
context.Product2.Where(..)。Tolist();
}
public List< Product3> GetProduct3Data(){
// ....
context.Product3.Where(..)。Tolist();
}
在调用产品时,我有一个WebApi方法可以接受产品类型并调用相应的方法:
public IHttpActionResult GetProducts(ProductType product)
{
/// ....
// Ii根据产品参数
}
实体调用库框架有什么办法可以选择只有一种方法的表?
您可以使用通用方法与接口约束
当您有这些自动生成的POCO类时:
public partial class Product1 {
public string Column1 {
get;
设置;
}
public string Column2 {
get;
设置;
}
}
public partial class Product2 {
public string Column1 {
get;
设置;
}
public string Column2 {
get;
设置;
}
}
public partial class Product3 {
public string Column1 {
get;
设置;
}
public string Column2 {
get;
设置;
}
}
您为实体类具有的属性创建一个界面通常...
interface IProduct {
string Column1 {
get;
设置;
}
string Column2 {
get;
设置;
}
}
...您应用于生成的类(在新的代码文件中,这些类是部分的,允许你这么做):
partial class Product1:IProduct {};
partial class Product2:IProduct {};
partial class Product3:IProduct {};
现在,您可以为查询创建一个通用方法。您可以将其作为扩展方法应用于您的 DbSet
s:
static class ProductExtensions {
public static List< T> GetProducts< T>(此DbSet< T>产品)
其中T:IProduct {
var productQry =
从产品中的产品
其中product.Column1 == 示例
选择产品;
return productQry.ToList();
}
}
您可以在 DbSet
s:
列表< Product1> product1List = context.Product1s.GetProducts();
列表< Product2> product2List = context.Product2s.GetProducts();
列表< Product3> product3List = context.Product3s.GetProducts();
您唯一的限制是表中的列确实需要具有相同的名称和类型。 EF不能识别显式的接口实现。另一方面,表格不一定完全相同。您可以为列的一部分定义一个界面(必须匹配),其余的可以不同。
I have a scenario where I have 3 types of products in a database and all products have their own separate tables (e.g. Product1
, Product2
and Product3
). Almost all of the product tables have the same schema. I have the requirement to get separate types of products in different tables.
I currently have 3 methods to get the products, one for each product type:
public List<Product1> GetProduct1Data() {
//....
context.Product1.Where(..).Tolist();
}
public List<Product2> GetProduct2Data() {
//....
context.Product2.Where(..).Tolist();
}
public List<Product3> GetProduct3Data() {
//....
context.Product3.Where(..).Tolist();
}
While calling products I have a WebApi method which accepts product type and calls the respective methods:
public IHttpActionResult GetProducts(ProductType product)
{
///....
// Ii have to call repositories according to product parameter
}
Does Entity Framework have any way that I can select a table with only one method?
You can use a generic method with an interface constraint.
When you have these auto-generated POCO classes:
public partial class Product1 {
public string Column1 {
get;
set;
}
public string Column2 {
get;
set;
}
}
public partial class Product2 {
public string Column1 {
get;
set;
}
public string Column2 {
get;
set;
}
}
public partial class Product3 {
public string Column1 {
get;
set;
}
public string Column2 {
get;
set;
}
}
You create an interface for the properties the entity classes have in common...
interface IProduct {
string Column1 {
get;
set;
}
string Column2 {
get;
set;
}
}
...which you apply to your generated classes (in new code files - the classes are partial to allow you to do that):
partial class Product1 : IProduct {};
partial class Product2 : IProduct {};
partial class Product3 : IProduct {};
Now you can create a generic method for your query. You can make it an extension method to apply it to your DbSet
s:
static class ProductExtensions {
public static List<T> GetProducts<T>(this DbSet<T> products)
where T : IProduct {
var productQry =
from product in products
where product.Column1 == "Example"
select product;
return productQry.ToList();
}
}
You can use this extension method on your DbSet
s:
List<Product1> product1List = context.Product1s.GetProducts();
List<Product2> product2List = context.Product2s.GetProducts();
List<Product3> product3List = context.Product3s.GetProducts();
The only restriction that you have is that the columns in your tables really need to have the same name and type. EF does not recognize explicit interface implementations. On the other hand, the tables don't have to be completely identical. You can define an interface for a part of the columns (which have to match) and the rest can be different.
这篇关于实体框架中多个类似实体类型的通用查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!