Connect to javasado. NET Entity Framework for the first time

Source: Internet
Author: User
Document directory
  • Establish Development Environment
  • Initial Release of the development interface
  • Access Entity-use Entity Client
  • Access Entity-use Object Service
  • Access Entity-use LINQ to Entities
  • Next step
Establish Development Environment

ADO. NET Entity Framework is.. NET Framework 3.5. Therefore, Visual Studio 2005 cannot be developed and must be supported by Visual Studio 2008.. NET 3.5 provides the latest version of Beta 2 and the built-in functions (the latest version is Beta 2, you can only publish Professional (the major version is lower than the major version and the Team Suite version ).

Visual Studio 2008 Professional Edition Beta 2: http://www.microsoft.com/downloads/details.aspx? FamilyID = b98a61ba-99b0-40b7-ab6e-5386a2b94217 & DisplayLang = en

Visual Studio 2008 Team Suite Beta 2: http://www.microsoft.com/downloads/details.aspx? Displaylang = zh-tw & FamilyID = 428c076f-e3ef-4290-9ff4-f6fd8f180b7d

If you use SQL Server 2005, would you need the following procedure Northwind Sample Database: http://www.microsoft.com/downloads/results.aspx? PocId = & freetext = Northwind & DisplayLang = en

After Visual Studio 2008 is installed on the server, you need to perform the following operations on ADO. NET Entity Framework:

1. ADO. NET Entity Framework Runtime libraries: http://www.microsoft.com/downloads/details.aspx? FamilyID = f1adc5d1-a42e-40a6-a68c-a42ee11186f7 & DisplayLang = en

2. ADO. NET Entity Framework Tools August CTP: http://www.microsoft.com/downloads/details.aspx? FamilyID = 09a36081-5ed1-4648-b995-6239d0b77cb5 & DisplayLang = en

After the installation is complete, you can start to develop the ADO. NET Entity Framework application.

Initial Release of the development interface

If you have installed Entity Framework Tools August CTP, you can see the example of ADO. NET Entity Data Model in the add case example window of Visual Studio:

This example stores the XML content of Conceptual Schema, Storage Schema, and Mapping Schema. After adding a Conceptual Schema, you can use [Open With…], Then select the XML Editor to open this case, and you can see the Schema content.

Select ADO. after the NET Entity Data Model, the Entity Data Model Wizard will appear. Currently, the Entity Data Model Wizard allows the Organization to access Data or create an Entity Model (that is, create a Schema manually ), if there is already a resource, it is easier to use the Wizard to generate an Entity Schema ratio directly from the resource category, however, if there is a natural source (that is, there may be different Data stores), you can choose to create it manually.

Entity Model Wizard automatically captures the data table, partition tables, and stored programs, and establishes the corresponding Schema, developers only need to establish a framework that can be directly used in the program.

After the Data Model is created in the Wizard, the Schema can be created. The creator can click the edmx tutorial. Visual Studio will introduce a graphic interface tool: entity Model Designer. With this tool, developers can easily construct their own Entity models without having to write a word or a word into the XML Schema. If you use the Model generated by the Data Pipeline, you can see the relationship between each Entity Model and it.

Entity Model Designer is a customizable user interface. developers can add/modify or delete Entity and Association directly on it, all modifications are directly rejected in the Entity Data Model.

Access Entity-use Entity Client

After the Entity Model is created, you can create an Entity program to access Entity. As mentioned in the previous article, developers can use the following three methods to access OSS.

The first is the EntityClient method, because it and ADO. NET object model is very similar, so it is useful over ADO. NET attackers are familiar with Connection, Command, and DataReader, but pay attention to the following points:

  • The settings of the Connection String are different. The previous Connection String is based on the used ADO. the NET resource provider is different. In EntityClient, the subject string of the Entity and Entity-related information must be applied. For example:

    string entityClientConnectionString =    @"Provider=System.Data.SqlClient; Metadata=.;    Provider Connection String='Initial Catalog=Northwind;    Integrated Security=SSPI'";

    Metadata =. it means that all Entity Model schemas are in the same category as the parent row, and the Provider is the data Provider, provider Connection String is the Connection String required by the data Provider to receive the data.

  • Currently, CommandBehavior of EntityDataReader only supports SequentialAccess. SequentialAccess can prevent the retrieval stream from being cached.

To access Entity through EntityClient, you need to use Entity SQL. This is a special version of Transact-SQL, which is used by the Entity to obtain the Entity, it can implement many functions that can be used in the data pipeline, but because it also has some restrictions, it needs to be optimized. One of the most explicit restrictions is that each data table must have a different name and be accessed with another name, for example, the following SQL statement:

SELECT CustomerID, ContactName FROM Northwind.Customers

It cannot be used. It must be used:

SELECT c.CustomerID, c.ContactName FROM NorthwindEntities.Customers AS c

.

With the above knowledge, you can export a simple EntityClient to access the data program:

static void Main(string[] args){    string entityClientConnectionString =        @"Provider=System.Data.SqlClient; Metadata=.;           Provider Connection String='Initial Catalog=Northwind; Integrated Security=SSPI'";    using (EntityConnection conn = new EntityConnection(entityClientConnectionString))    {        conn.Open();        EntityCommand cmd = new EntityCommand(            "SELECT c.CustomerID, c.ContactName FROM NorthwindEntities.Customers AS c",            conn);        EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);                while (reader.Read())            Console.WriteLine(                "CustomerID: " + reader.GetValue(0).ToString() +                 "  Customer Name:" + reader.GetValue(1).ToString());        reader.Close();        conn.Close();    }}

Does it look like accessing data from ADO. NET? If you are a developer who is familiar with ADO. NET but not familiar with Entity Framework, the suggestion can be written to the producer.

Access Entity-use Object Service

Object Service is a method that is more advanced than EntityClient. It can use Entity SQL to check the token, but use... Each... Instead of DataReader, therefore, you can use the object metadata Method for access.

In Object Service, each Entities (the set of Entity, which is EntityContainer in CSDL) is a stand-alone Entity, just like a Connection, objectQuery <T> is a Command, which also handles the dynamic query,

using (NorthwindEntities context = new NorthwindEntities()){    string esql =     "SELECT VALUE Customers FROM NorthwindEntities.Customers AS Customers";    ObjectQuery
 
   query =     new ObjectQuery
  
   (esql, context, MergeOption.NoTracking);
      foreach (Customers customer in query)        Console.WriteLine("CustomerID: " + customer.CustomerID +                " Contact Name:" + customer.ContactName);}
 

Object Service is more simple than Entity Client, because only one ObjectQuery <T> can be used to access the Entity Data, however, you must note that the Entity SQL here must be an Entity, that is, you cannot use it like this:

SELECT c.CustomerID, c.ContactName FROM NorthwindEntities.Customers AS c

Instead, use:

SELECT VALUE c FROM NorthwindEntities.Customers AS c

The VALUE command indicates that only one category is handled, and each column has only one specified category.

If you want to access the Property in Entity in Object Service, you must use IExtendedDataRecord to access it. For example, modify the previous example:

using (NorthwindEntities context = new NorthwindEntities()){    string esql =     "SELECT c.CustomerID, c.ContactName FROM NorthwindEntities.Customers AS c";    ObjectQuery< IExtendedDataRecord> query =     new ObjectQuery< IExtendedDataRecord >(esql, context, MergeOption.NoTracking);    foreach (IExtendedDataRecord reader in query)        Console.WriteLine("CustomerID: " + reader.GetValue(0).ToString() +                " Contact Name:" + reader.GetValue(1).ToString());}
Access Entity-use LINQ to Entities

Then, we can use the LINQ in C #3.0 (VB 9.0) to access the Entity. First, we need to first understand what the LINQ is, in fact, it refers to the collection of data in the program, and uses a method similar to SQL to query data. In the past, we had to make a circle, or call the INS ins () method provided by the set to determine the condition. In LINQ, we can search for the information in the set using the simple class lookup method, and extract the qualified parts.

LINQ to Entities is ADO. NET Entity Framework is opened to the method for querying objects in LINQ. As long as you have learned the method of compiling in LINQ, then you can understand the interfaces opened by the interfaces in the LINQ to Entities, in this way, you can simply query the information in Entity, for example:

using (NorthwindEntities db = new NorthwindEntities()){    IQueryable<Products> query =       db.Products.Where(p => p.UnitPrice > 10 && p.UnitPrice < 30);    foreach (var product in query)    {        Console.WriteLine(            "ProductID: " + product.ProductID +             " Name: " + product.ProductName +             " Price: " + product.UnitPrice);    }}

It is very simple, and there are no SQL commands, so the Entity SQL is no longer used.

Every EntityContainer is a Database, and the query result is passed through the commands under EntityContainer, which is developed by the LINQ to Entities, the result of each query is handled by IQueryable. developers can use foreach to modify IQueryable.

The following is an example of how to query the zookeeper table:

using (NorthwindEntities db = new NorthwindEntities()){    ObjectQuery<Invoices> query = db.Invoices;    IQueryable<Invoices> result = from q in query                             select q;    foreach (var invoice in result)    {        Console.WriteLine(            "CustomerID: " + invoice.CustomerID +            " ProductID: " + invoice.ProductID +            " Name: " + invoice.ProductName +            " Price: " + invoice.UnitPrice);    }}
Next step

This article describes the initial scheme of using ADO. NET Entity Framework's basic practices and simple query functions, of course not only. We will introduce them in the future, for example, the ability to parse metadata and Transaction also has more efficient Meta services and interface query methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.