什么是"它"在实体框架 [英] What is "it" in Entity Framework

查看:167
本文介绍了什么是"它"在实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我,如果这已被要求,但它没有出现在我的任何搜索之前。我有两个数据库表人士及员工建模表每类型(例如员工是 - 一个人)。在我的EDMX设计师,我已经定义了每列映射到它们各自的底层表中的单个实体的雇员。(如:名称 - >人员,工资 - >员工)



它让我做这样的东西在LINQ表达式:

  context.Employees.Where(it.Name LIKE'M %'AND it.Salary> 1234) 

是否有良好的联系解释我怎么能指望它表现? 。我相信这不是一个普通的LINQ的东西,它是有点具体到实体框架



编辑0:生成的C#的ObjectContext的代码如下:

 公共部分类TestObjectContext:ObjectContext的
{
//大量的样板为清楚起见,删除

酒店的公共对象集<员工>员工
{
得到
{
如果((_Employees == NULL))
{
_Employees = base.CreateObjectSet<员工>(员工 );
}
返回_Employees;
}
}
}


解决方案

默认别名作为电流的ObjectQuery 命令。请参阅查询生成器方法文档,尤其是别名部分:



查询构建器的方法依次施加以构建累积的查询命令。这意味着,在当前的ObjectQuery命令等来施加电流的方法的子查询处理。



在一个查询生成器方法,通过使用别名引用当前的ObjectQuery命令。默认情况下,字符串它是表示当前命令,如下面的例子别名:

  INT成本= 10; 
//返回的产品对象与标准成本
//高于10美元。
的ObjectQuery<产品与GT; productQuery =
context.Products
。凡(it.StandardCost> @cost,新的ObjectParameter(成本,成本));

当您设置的ObjectQuery的Name属性,该值成为后续方法的别名。

 <$ C $:以下示例通过设置的ObjectQuery为产品的名称,然后在随后的排序依据的方法使用该别名扩展前一C> //设置查询的名称属性,然后
//用该名称作为在随后的
//排序依据的方法的别名。
productQuery.Name =产品;
的ObjectQuery<产品与GT; filteredProduct = productQuery.OrderBy(product.ProductID);


Forgive me if this has been asked before but "it" doesn't come up in any of my searches. I have two database tables Person and Employee modeling a Table-per-Type (e.g. Employee is-a Person). In my edmx designer I have defined a single entity Employee that maps each column to their respective underlying table (e.g. Name -> Person, Salary -> Employee).

"it" allows me to do stuff like this in a LINQ expression:

context.Employees.Where("it.Name LIKE 'M%' AND it.Salary > 1234")

Are there any good links explaining how I can expect "it" to behave? I presume it's not a generic LINQ thing and that it is somewhat specific to Entity Framework.

EDIT 0: The generated C# code for the ObjectContext follows:

public partial class TestObjectContext : ObjectContext
{
  // lots of boilerplate removed for clarity

  public ObjectSet<Employee> Employees
  {
    get
    {
      if ((_Employees == null))
      {
        _Employees = base.CreateObjectSet<Employee>("Employees");
      }
      return _Employees;
    }
  }
}

解决方案

it is the default alias for the current ObjectQuery command. Please refer to the documentation for Query Builder methods, especially the Alias section:

Query builder methods are applied sequentially to construct a cumulative query command. This means that the current ObjectQuery command is treated like a sub-query to which the current method is applied.

In a query builder method, you refer to the current ObjectQuery command by using an alias. By default, the string "it" is the alias that represents the current command, as in the following example:

int cost = 10;
// Return Product objects with a standard cost
// above 10 dollars.
ObjectQuery<Product> productQuery =
    context.Products
           .Where("it.StandardCost > @cost", new ObjectParameter("cost", cost));

When you set the Name property of an ObjectQuery, that value become the alias in subsequent methods. The following example extends the previous one by setting name of the ObjectQuery to "product" and then using this alias in the subsequent OrderBy method:

// Set the Name property for the query and then 
// use that name as the alias in the subsequent 
// OrderBy method.
productQuery.Name = "product";
ObjectQuery<Product> filteredProduct = productQuery.OrderBy("product.ProductID");

这篇关于什么是&QUOT;它&QUOT;在实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆