EF Entity Framework

Source: Internet
Author: User

EF Entity Framework

What is Entity? I believe everyone knows it. When we reconstruct the data center charging system, the layer responsible for data transfer between the three layers. How is the data type defined in a scope.


I. What is the Entity Framework?


Entity Framework is a setTechnology that supports developing data-oriented software applicationsIs An ORM framework of Microsoft. EF is a technical database table and column that allows developers to use data in the form of domain-specific objects and attributes without opening the way to store data. A higher level of abstraction is formed, and less code can be used than traditional applications.


2. You may ask again what is the orm framework?


In a broad sense, ORM refers to the mutual conversion between the object-oriented object model and the data structure of relational databases. In a narrow sense, ORM can be considered as a virtual Object-oriented Data access interface based on relational database data storage. Ideally, based on such an object-oriented interface, persistence of an OO object requires no understanding of the Implementation Details of any relational database storage data.Conversion between table entities and tables. ConvertBody changes are mapped to the table.


[Meaning of three letters]

Iii. How does EF work?


The abstract data structure is used to convert each database object into an application object, and the data field becomes an attribute, and the link becomes a combination attribute, turn the ER model completely into a database object model. In this way, programmers can operate on these objects in the most familiar language. In fact, they are operating on Database changes. In the abstract structure, combined with a three-layer framework, data access can be easily implemented.


4. There are two common methods to define a data model:


1. CodeFirst

Use Code First to define a model and then generate a database.
CodeFirst is a technical method of EntityFramework, because the traditional programming method is to first establish a database, then model the application based on the database model, and then develop. CodeFirst is literally the code first, first, create the object structure to be mapped to the database in the program, and then the EntityFramework can generate the corresponding database according to the object structure.


2. ModelFirst

Model First is used to define the Model with boxes and lines, and then generate a database.

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


V. ModelFirst

1. Create an ADO. NET Object Data Model


2. Add an object


3. Generate a database based on the model



4. Test Database Connection



5. Database generation generates the corresponding database script language.




Example CodeFirst:

Codefirst uses the context DBContext object for database access. Query data in combination with Linq.


namespace CodeFirstDatabaseSample{    class Program    {        static void Main(string[] args)        {            using (var db = new BlogContext())            {                Console.Write("Enter a name for a new blog:");                var name = Console.ReadLine();                var blog = new Blog { Name = name };                db.Blogs.Add(blog);                db.SaveChanges();                var query = from b in db.Blogs                            orderby b.Name                            select b;                foreach (var item in query)                {                    Console.WriteLine(item.Name);                }            }        }        public class Blog        {            public int BlogId { get; set; }            public string Name { get; set; }            public virtual List<Post> Posts { get; set; }        }        public class Post        {            public int PostId { get; set; }            public string Title { get; set; }            public string Content { get; set; }            public int BlogId { get; set; }            public virtual Blog 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, facilitating database modification and other operations. Make data access easier.



Related Article

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.