Entity Framework 6 Recipes translation series (2), entityframework

Source: Internet
Author: User

Entity Framework 6 Recipes translation series (2), entityframework

Visual Studio

The tools we use to develop applications on Windows platforms are Visual Studio. this integrated development environment has evolved for many years from a simple C ++ editor and compiler to a multilingual environment that is highly integrated and supports the entire lifecycle of software development. Visual Studio and Its released tools and services provide: design, development, unit testing, debugging, software configuration and management, build management and continuous integration. Few developers are worried about not using it (note: the author should be concerned about the power of VS). Visual Studio is a complete tool set. Visual Studio plays a vital role in developing applications of the application Entity Framework.

Visual Studio provides an integrated designer for Entity Framework models. You can use the tools provided by Visual Studio to create models from the beginning or from an existing database. Of course, you can choose not to use the designer to create the object type and configuration manually.

In most cases, we have an existing application and database. Visual Studio provides a tool for us to import tables and relationships in the database to the object framework model. This is very suitable for us, because few will choose to develop a brand new application. In most cases, we maintain, expand, and improve an existing code and database.

You can create an empty model in the calculator, and then add the entity types, associations between types, and inheritance systems to the designer from the beginning. After creating a model, you can right-click the designer and select "create database from model" from the menu.

If your project team is code-centric, You can manually create a series of domain classes, their relationships, and context objects without using the designer, then, associate these classes with the Entity Framework engine.

Once a model is created, changes often occur, because this is a natural thing for software development. Visual Studio provides tools to help us update our model from the database, which will synchronize the model with the database. In addition, the Entity Framework Development Team provides us with a Code First Migrations tool that keeps the database updated in your model.

1-2 use the Entity Framework

The Entity Framework is tightly integrated with Visual Studio. To use the Entity Framework in your application, we add an ADO. NET Entity Data framework to your project. Right-click your project and select New Item ). In the pop-up dialog box (1-4), select the ADO. NET Entity Date Model (Entity Data Model) Template under the Data (Data) template. Click Add to open the data model creation guide.

Figure 1-4 Add an object data model to a project

The wizard on the first page has two options: start with an existing database (created from an existing data); start with an empty model (create an empty model ). (The option label of the former is actually "Generate from database"), 1-5.

Figure 1-5 select an empty model from an existing database in the Object Data Model Wizard

Creating a model from an existing Database is a Database-First solution. From the tables, views, and stored procedures of your database, the wizard creates a model and an entity class for encoding. The immediate benefit is that you can use the Entity Framework in encoding to map tables and columns in the database to strongly typed entity classes in the model. If tables in the database are related, they are modeled as associations. If you have an existing database, this is a way to create an object data model for your application. However, if you like to use the Code First solution to create a model from an existing database, don't worry, the Entity Framework Development Team has created a powerful tool set (The Entity Framework Power Tools), which uses reverse engineering to create a domain Entity class from an existing database, just as you typed it out manually.

If you develop a brand new application without an existing database, you can also select the Entity Framework designer. First, create an empty model, and then right-click to create the object type, association, and inheritance. You can also drag them from the toolbox to the designer to create them. Once the modeling is complete, you can right-click the menu item of the designer and select Generate Database from Model (create Database from Model. It generates a script for creating database tables and relationships.

You can also manually create each object class in Visual Studio, and then register them in the DbContext context object to connect to the object Framework Service.The Entity Framework maps these entity classes to the underlying database and automatically creates a model in the memory at runtime.

Model-First or Database-First, and use the Entity Framework designer for modeling. The key points are as follows: 1-6. In this model, the Customer and Order have one-to-multiple associations. Each Customer may have multiple Orders, but each Order can be associated with only one Customer. the ing details window displays the ing of an object-type Customer to the Customer table in the underlying database. This window also shows Scalar Proerties. columns in the Customer table are mapped to the Scalar attribute (Scalar Proerties) in the object type Customer ). remember, in the Entity Framework, you can use the data annotation or the API feature in Code First to create the same ing.

Figure 1-6 key points in designer Modeling

Of course, the key points in designer modeling are far more than those shown in Figure 1-6. This book covers all aspects of designer modeling. In some cases, beyond the capabilities of the designer, this book will show how to handle this situation by modifying the underlying. edmx file. This file contains the complete model definition, including the concept layer, storage logic layer, and ing layer.

So,Whether we use Database-First, Model-First, and Code-First in the Entity Framework, I will eventually use a Model.. We achieved high productivity, because during encoding, we can use models (entity classes) like other common objects in an application, as shown in 1-6. In many cases, you can use the Customer and Order objects like other common objects in an application.

If you want to insert a new customer and order to the database, you can create an instance of the Customer and Order Types and set their attributes. Then add it to the context object that represents the model in the memory. Call the Savechanges () method to save the changes. The required SQL statement is automatically generated and sent to the database to insert a row. Obtain the customers and orders in the database. You can create a query using LINQ or SQL based on the entity types and associations in the model.

The skills throughout this book will show you step by step how to model scenarios in almost all the databases you can think of; how to query, insert, update, and delete models. And how to use the Entity Framework in a variety of applications.

The first chapter ends here.If you think it is good, please click the recommendation in the lower right to share it with more people.

 

 

Entity Framework exchange QQ group: 458326058. You are welcome to join us.

Thank you for your continued attention, my blog address: http://www.cnblogs.com/VolcanoCloud/

 

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.