简体   繁体   English

具有不同名称的实体框架查询表

[英]Entity Framework query table with different name

I have a few tables which are exactly the same (except the table name). 我有一些完全相同的表(表名除外)。 The data are different, and due to some specific reason, they cannot be merged into one single table . 数据是不同的,并且由于某些特定原因,它们无法合并到一个表中

Example: 例:

TableA (
    Value1 INT NULL,
    Value2 INT NULL
)

TableB (
    Value1 INT NULL,
    Value2 INT NULL
)

.... etc ....

I need to perform same complex calculations / operations to all the tables (which are exactly the same). 我需要对所有表执行完全相同的复杂计算/操作(完全相同)。 How can I do that with EF? 我该如何使用EF?

In traditional SQL query, we can construct query string by altering the FROM < table name > . 在传统的SQL查询中,我们可以通过更改FROM < 表名 >来构造查询字符串。 But I can't figure how I can do that with EF 但是我不知道如何用EF做到这一点

Note: EF model-first approach 注意:EF模型优先方法

Thanks in advance 提前致谢

This question might be a possible duplicate Here is the link, 这个问题可能是重复的问题。 这里是链接,

You can use above post to get the list of entities you want. 您可以使用上面的帖子获取所需的实体列表。

Try this query when you have the list of entities you want 当您拥有所需的实体列表时,请尝试此查询

      foreach (var table in entities)
        {
          var record =  (from x in table 
                where x.Value1 == 1
                select new CommonTableModel()
                {
                          Value1  = x.Value1 , 
                          Value2  = x.Value2 
                }).ToList();
        }
  1. Inherit both classes( Employer , Customer ) from common parent class - Person . 同时继承类( EmployerCustomer从普通的父类) - Person
  2. All common methods should become generic with constraint - where T : Person , this way you will have access to all properties of both classes inside these methods. 所有通用方法都应成为具有约束的泛型- where T : Person ,这样,您将可以访问这些方法中两个类的所有属性。

Implementation: 实现方式:

public abstract class Person
{
    public int Id {get;set;}
    public string Name {get;set;}
    public int Age {get;set;}
}

public class Employer : Person
{}

public class Customer : Person
{}

public List<T> GetPersonsOlderThan20Years<T>() where T : Person, new()
{
    //any common logic
    return context.Set<T>().Where(x => x.Age > 20).ToList();
}

Usage: 用法:

var employers = GetPersonsOlderThan20Years<Employer>();
var customers = GetPersonsOlderThan20Years<Customer>();

create a generic entity 创建一个通用实体

for specific row in an entity 对于实体中的特定行

//int id or whatever your [key] attribute is
public T GetSelected<T>(int id) where T : class
{
    return _dbContext.Set<T>().FirstOrDefault(x => x.Id == id);
}

var selectedFromEntity1 = GetSelected<Entity1>(idOfEntity1);
var selectedFromEntity2 = GetSelected<Entity2>(idOfEntity2);

for all the rows of an entity 对于实体的所有行

public List<T> GetAll<T>() where T : class
{
    return _dbContext.Set<T>().ToList();
}

var dataFromEntity1 = GetAll<Entity1>();
var dataFromEntity2 = GetAll<Entity2>();

or if you want to add paging or conditions 或者如果您想添加分页或条件

public IQueryable<T> GetAll<T>()
{
   return _dbContext.Set<T>().AsQueryable();
}


var dataFromEntity1 = GetAll<Entity1>();
var resultFrom1 = dataFromEntity1
                     .Where(x => x.Property1 == "searchFilter") //user input searchFilter
                     .Skip(pageSize* pageNumber) //user input page size and page number
                     .Take(pageSize) //user input page size
                 .ToList();

var dataFromEntity2 = GetAll<Entity2>();
var resultFrom2 = dataFromEntity2
                     .Where(x => x.Property1 == "searchFilter") //user input searchFilter
                     .Skip((pageSize * pageNumber)-pageSize) //user input page size and page number
                     .Take(pageSize) //user input page size
                 .ToList();

I think raw queries are your anser: Raw SQL 我认为原始查询是您的烦恼: 原始SQL

So you can basically: 因此,您基本上可以:

   using (var context = new BloggingContext()) 
   {
      var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList(); 
   }

And exchange the name of the table as you suggested. 并按照您的建议交换表的名称。

Note: I have not tried this, and for sure the tables must be completely identical (even column ordering) 注意:我还没有尝试过,并且确保表必须完全相同(甚至列顺序)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM