Entity Framework Foundation

Source: Internet
Author: User

Spent 5, 6, and July on a busy day, and now take some time to make some basic memories of the use of the Entity Framework.

What is the Entity Framework?

The Entity Framework (EF), like our familiar ADO, provides objects for accessing and manipulating databases, but they are also different.

Ado. NET provides a set of object model for the operation of the underlying database, the abstraction level is lower, the operation efficiency and performance is high, is the common way to access the database in daily development.

EF has a higher level of abstraction: it maps the database to DbContext, transforms the database objects into application objects (entity), and the data fields are converted to properties, and the relationships are converted to associative attributes (association). Improved efficiency in system development, and EF makes it easy to upgrade easy-to-maintain, easy-to-scale systems.

EF also supports LINQ, LAMBDA writing queries and other related database operations, without the need to understand the SQL syntax and skilled operation of the database.

Because EF converts database objects into physical objects stored in memory, EF uses the process as a comparison of memory. Plus the high level of abstraction in the access to the underlying database model needs to be converted, in the database operation performance generally not ADO high. This is the drawback of enjoying ef.

EF is primarily a: *.EDMX file in the project. EF is divided into three main layers:

(1) The conceptual model (conceptual model): The main embodiment is a set of classes that can be used directly by the application. These classes are also classes that we use directly in our programs, often referred to as "entities"

(2) storage model (Storage models): A group of classes that correspond directly to the underlying data storage media, such as a database system.

(3) Concept-storage model mapping (Conceptual-storage Mapping) that solves the problem of how classes in the conceptual model correspond to classes in the storage model.

Entity Framework Creation

1. Create the EF file in VS2012 and select the database first development mode. 、

2. Configure the database that EF needs to connect to.

3. After the configuration is complete, we can see the true colors of EF. On the left is the map of the database corresponding table.

4. In the left margin right--"map Details" can query the details of the database mapping table can be modified, "model Browser" can query to the EF three layer structure: conceptual model, concept-storage model mapping, storage model.

The Entity Framework uses crud

1. Add Data

New websitedbentities ();
New Users () { "9245162", "10003" , "123456", true}; Entity. Users.add (user), entity. SaveChanges ();

Execution Result:

2. Querying data

//Linq QueryvarQuery_user_to_linq = fromCinchEntity. UserswhereC.account = ="9245162" SelectC;foreach(varUsersinchquery_user_to_linq) Console.WriteLine ("Linq Query Result: Account number: {0}, encoding: {1}", user. Account, user. Code);//LAMBDA QueryvarQUERY_USER_TO_LAMBDA = entity. Users.where (c = C.account = ="9245162"). ToList ();foreach(varUsersinchQuery_user_to_lambda) Console.WriteLine ("Lambda Query Result: Account number: {0}, encoding: {1}", user. Account, user. Code);
//SQL QueryvarQuery_user_to_sql = entity. Users.sqlquery ("Select * from Users Where account= ' 9245162 '"). ToList ();foreach(varUsersinchquery_user_to_sql) Console.WriteLine ("SQL Query: Account number: {0}, encoding: {1}", user. Account, user. Code);

Execution Result:

3. Modify the data

" 10003 "  "CK9245162"= system.data.entitystate.modified;entity. SaveChanges ();

4. Delete data

" 9245162 " ); entity. Users.remove (duser); entity. SaveChanges ();

Three development models of Entity Framework

DataBase First: The traditional table-driven approach creates the EDM and then generates the model and data-tier code through the EDM.
Model first: Create the EDM model before generating DDL database scripts and model and data layer code.
Code First: manual creation of POCO models, data-tier dbcontext and mapping relationships, and the creation of databases through Database.setinitializer, which is more flexible, but it works more.

Entity Framework Foundation

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.