First knowledge of ENTITYFRAMEWORK6

Source: Internet
Author: User

First knowledge of ENTITYFRAMEWORK6 What is EF?

EF is an ORM (object-relational mapping) framework that allows us to map objects to the underlying database structure when we are programming. For example, you can create an order table in a database that maps to the order class in your program, so that each order object in the program corresponds to a record in the order table, which is responsible for converting the recordset from the database back to the object. The corresponding SQL commands can be generated according to the object's current state, and the data will be accessed (the common data access operations can be referred to as crud:create, Read, Update, Delete).

Three ways to develop EF

EF supports three development models:CodeFirst,Database First, and modelfirst.

Mode one: Code first

EF's code first is a bit of a magic color for people who are on the initial contact. Let's take a look at the following. Create two classes: Book and Bookreview (review). A book can have many book reviews, so they are a one-to-many relationship:

  1. Public class Book
  2. {
  3. Public Virtual int Id {get; Set ;}
  4. Public Virtual string Name { get; Set ; }
  5. Public Virtual List<bookreview> reviews { get; Set ; }
  6. }
  7. Public class Bookreview
  8. {
  9. Public int Id{get; Set ;}
  10. Public int BookId { get; Set ; }
  11. Public Virtual string Content { get; Set ; }
  12. Public Virtual Book Assoicationwithbook { get; Set ; }
  13. }

OK, now create a subclass derived from DbContext:

  1. public class BookDb : DbContext
  2. {
  3. public DbSet<Book> Books { get; set; }
  4. public DbSet<BookReview> Reviews { get; set; }
  5. }

You can now write a few lines of code in your program to extract data from the database:

  1. Static void Main (string[] args)
  2. {
  3. using (var context = new bookdb())
  4. {
  5. Console. WriteLine ("There are {0} booksin the database",context. ) Books. Count ());
  6. }
  7. }

To run, if you have SQLExpress installed on your computer, either in the Application folder, or by opening SQL Server Management Studio (SSME) to view native SQL Server, you will find that the database has been created. The tables and tables associated with them also help you to complete the following:

Run Results

It seems that I did nothing, everything is OK!

Mode two: Database first

This is the feature that EF has supported since 1.0, with the idea of designing and building a database, and then using Visual Studio's wizards to create the EF data model and generate the entity class code. This is the most mature and stable way, its designer is quite perfect, basically can meet the various needs in the actual development. I personally think this is the most appropriate way to develop a formal project.

mode three: Model first

This pattern is to create the entities and their associations in the visual designer first, and then the designer generates the SQL commands and saves them in a SQL file to complete the creation and modification of the database by executing the SQL file.

Model Designer

PS: It seems and is canceled in EF7, so it is not recommended to use it again.

The above section extracts from Bitfan's column

the Entity framework to grasp the global
Compare Codefirst and Database first

It's easy to build a database with Code first, but the data table structure that EF has built for us is really generic. In the example above, the Name,Content, andBookId in the table generated by Code first can be empty, that is, they are not constrained. It's a bit of a hassle to use code first to build constraints , so we don't usually generate the database by using the codes, but instead generate the codein reverse from the database.

This section is just an introduction, and I'll show you how to use EF to connect to a database, especially a MySQL database, in the next section. EF connects to SQL Server through the wizard is basically the next step, but even MySQL requires some configuration.

Catalog: EntityFramework6 Getting Started tutorial

Next section: Connecting MySQL with EntityFramework6

First knowledge of ENTITYFRAMEWORK6

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.