简体   繁体   English

ADO.Net 实体框架:这是什么?

[英]ADO.Net Entity Framework: What's this?

I'm just starting developing with ADO.NET Entity Framework and I don't know how I would write this:我刚刚开始使用 ADO.NET 实体框架进行开发,但我不知道如何编写:


ClientEntities clientContext = new ClientEntities();

   ObjectQuery clientQuery = 
        clientContext.Client.Where("it.email = @email AND it.password = @password",
                    new ObjectParameter("email", "xxx@hotmail.com"),
                    new ObjectParameter("password", "xAdxar12s"));

            Console.WriteLine(clientQuery.Count());
            Console.ReadLine();

And it works!它有效!

But, what's this?但是,这是什么?


"it.email = @email AND it.password = @password"

Where can I learn this "SQL syntax"?我在哪里可以学习这种“SQL 语法”? I don't know why I have to use it.email or the meaning of SELECT VALUE .我不知道为什么我必须使用它。 emailSELECT VALUE的含义。 It is "Entity SQL"?它是“实体 SQL”吗?

Thanks!谢谢!

Yes, this is Entity SQL.是的,这是实体 SQL。

"LINQ to Entities" is one way of retrieving entity instances from your context. “LINQ to Entities”是从上下文中检索实体实例的一种方式。 Entity SQL is another.实体 SQL 是另一个。 Both work, are supported, and complement each other.两者都工作,得到支持,并相互补充。

SELECT VALUE means "this statement should return entity instances." SELECT VALUE 的意思是“这个语句应该返回实体实例”。 (As opposed to a row wrapper.) You'll get an object query back. (与行包装器相反。)您将返回一个 object 查询。

Now, what is "it"?现在,什么是“它”? Well, Client is a property of the generated ObjectContext, of type ObjectQuery<Client>.嗯,Client 是生成的 ObjectContext 的属性,类型为 ObjectQuery<Client>。 You can look at the source code for Client in the codegen file for the model (the file with the extension.Designer.cs):您可以在 model(扩展名为.Designer.cs 的文件)的 codegen 文件中查看 Client 的源代码:

    /// <summary>
    /// There are no comments for Client in the schema.
    /// </summary>
    public global::System.Data.Objects.ObjectQuery<Client> Client
    {
        get
        {
            if ((this._Client== null))
            {
                this._Client = base.CreateQuery<Client>("[Client]");
            }
            return this._Client;
        }
    }
    private global::System.Data.Objects.ObjectQuery<Client> _Client;

Do you see the Entity SQL there?您在那里看到实体 SQL 吗? It's easy to miss if you don't look carefully.不仔细看很容易错过。 I'll copy it down here:我将它复制到这里:

"[Client]"

I can't find the documentation for this syntax, but that's mostly because searching for square brackets and NOT any other Entity SQL keyword is sort of difficult to do with most search engines.我找不到此语法的文档,但这主要是因为搜索方括号而不是任何其他实体 SQL 关键字对于大多数搜索引擎来说有点困难。 Like T-SQL, the square brackets appear to mean "consider everything in between these as an identifier."与 T-SQL 一样,方括号似乎意味着“将它们之间的所有内容视为标识符”。 But the statement appears to be shorthand for:但该声明似乎是以下的简写:

 "SELECT VALUE it FROM MyEntities.[Client] AS it"

So now you know where "it" comes from.所以现在你知道“它”是从哪里来的了。

Where did you get that sample from?你从哪里得到那个样本? the normal way of doing this would be:这样做的正常方法是:

string email = "xxx@hotmail.com",
       pw = "xAdxar12s";
var qry = from client in clientContext.Client
          where client.email == email
             && client.password == pw
          select client;

Console.WriteLine(qry.Count());

That gives you static typing, etc - and is just friendlier!这给了你 static 打字等 - 而且更友好!

(note: check you have using System.Linq; at the top of the file) (注意:检查您是否using System.Linq;在文件顶部)

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

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