简体   繁体   中英

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:


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"? I don't know why I have to use it.email or the meaning of SELECT VALUE . It is "Entity SQL"?

Thanks!

Yes, this is Entity SQL.

"LINQ to Entities" is one way of retrieving entity instances from your context. Entity SQL is another. Both work, are supported, and complement each other.

SELECT VALUE means "this statement should return entity instances." (As opposed to a row wrapper.) You'll get an object query back.

Now, what is "it"? Well, Client is a property of the generated ObjectContext, of type 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):

    /// <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? 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. Like T-SQL, the square brackets appear to mean "consider everything in between these as an identifier." 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!

(note: check you have using System.Linq; at the top of the file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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