The EF Entity Framework First understands

Source: Internet
Author: User

What is the entity (entities), I believe we all know, is that we reconstruct the computer room charge system, that in the three layer is responsible for data transmission layer. How the data type is defined within a scope.


what is the Entity Framework (EntityFramework)?


The Entity framework is a set of technologies in ADO that support the development of data-oriented software applications and is an ORM framework for Microsoft. EF is a technical database table and column that develops data that can be used in the form of domain-specific objects and attributes without open-circuit storage . Forms a higher level of abstraction and can be less code than a traditional application.


second, perhaps you will ask, what is this ORM framework?


In broad sense, ORM refers to the mutual transformation between object-oriented object model and relational database data structure. In the narrow sense, ORM can be considered as data storage based on relational database, which is a virtual object-oriented data access interface. Ideally, based on such an object-oriented interface, persisting an Oo object should not require understanding the implementation details of any relational database storage data. the mutual transformation of the table entity and table. Map The changes of the solid to the table.


[three-letter meaning]

third, how does EF work?


Using the method of abstract data structure, each database object is converted into an Application object, and the data field becomes the attribute, the relationship becomes the binding attribute, and the ER model becomes the database object model completely. This allows programmers to manipulate these objects in the most familiar language, in fact manipulating database changes. In the abstract structure, combined with a three-layer framework to make data access easy to implement.


There are two common ways to define a data model:


1, Codefirst

Use code first to define the model and then build the database.
Codefirst is a technical means of entityframework, because the traditional programming method is to build the database first, then model the application according to the database model, and then develop Codefirst is literally the code First, establishing the entity structure to be mapped to the database in the program, and then EntityFramework can generate the corresponding database from the root entity structure.


2, Modelfirst

Use model first to define the model with boxes and lines, and then build the database.

With model first, you can use the Entity Framework designer to design a new model, and then generate the database schema from the models. The model is stored in an edmx file (with an. edmx extension) and can be viewed and edited in the Entity Framework designer. The classes used in the application for interaction are automatically generated from the EDMX file.


v. Examples of Modelfirst

1. Create an ADO Entity Data model


2. Adding entities


3. Database generation based on model



4. Test database connection



5, database generation, the corresponding database scripting language will be generated.




Example Codefirst:

Codefirst needs to use the context DbContext object accessed by the database. Queries that combine data with LINQ.


Namespace codefirstdatabasesample{    class program    {        static void Main (s Tring[] args)         {            using (var db = new Blogcontext ()) &nbs P           {                Console.Write ("Enter a name fo  R A new blog: ");                var name = Console.ReadLine ();                var blog = new Blog {name = name};              & nbsp Db. Blogs.add (blog);                DB. SaveChanges ();                var query = from B in db. blogs                            B.NAME&NBSP ;                          Select b;                foreach (var item in query)       &N Bsp         {                  Console.WriteLine (item. Name);               }           }    &N Bsp  }        public class blog        {          &NBSP ; public int BlogId {get; set;}             public string Name {get; set;}             public virtual list<post> Posts {get; set;}        }        public class post        {    &NBSP ;       public int PostID {get; set;}             public string Title {get; set;}             Public StRing Content {get; set;}             public int BlogId {get; set;}             public virtual blog {get; set;}        }        public class blogcontext:dbcontext        {             public dbset<blog> Blogs {get; set;}             public dbset<post> Posts {get; set;}        }   }}


Vi. Summary


The Entity Framework Abstracts database objects, which facilitates our operations on database modifications. Make the data access work simple.



The EF Entity Framework First understands

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.