Entity Framework 6 Recipes Translation Series 2-----The first chapter to start using solid frame 2

Source: Internet
Author: User

Visual Studio

The tools we use to develop our applications on the Windows platform are primarily visual Studio. This integrated development environment has evolved over many years, from a simple C + + editor and compiler to a multi-lingual environment that is highly integrated and supports the entire lifecycle of software development. Visual Studio and the tools and services it publishes provide: design, development, unit testing, debugging, software configuration and management, build management, continuous integration, and more. Few developers are worried about not using it (note: the author should be able to express the ability not to worry about Vs), Visual Studio is a complete toolset. Visual Studio plays a critical role in developing applications that apply entity frameworks.

Visual Studio provides an integrated designer for the Entity Framework model, using the tools it provides that you can create from scratch or from an existing database. Of course, you can choose to create entity types and configurations without using the designer, purely manually.

In most cases, we have an existing application and database, and VisualStudio provides us with tools that can help us to import the tables and relationships in the database into the Entity Framework model. This is perfect for us, because few people will choose to develop a brand new application. In most cases we maintain, extend, and improve an existing code and database.

You can create an empty model in the meter and then start from scratch to increase the entity type, the association between the types, and the inheritance system into the designer. After you create a good model, you can right-click the designer and choose Create Database from the model from the menu.

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

Once the model has been created, the changes are frequent, because it is a natural thing to do with software development. Visual Studio provides tools to help us update our model from the database, which synchronizes the model with the database. In addition, the Entity Framework development team has provided us with a code first migrations tool that keeps the database up to date with the changes in your model.

1-2 using the Entity Framework

  The Entity Framework is tightly integrated with Visual Studio, and in order to use the Entity Framework in your application, we add an ADO Entity Data framework to your project. Here's how: right-click your project and choose? New Item. In the dialog box that pops up (1-4), select the ADO Date Model (Entity Data Model) template below the data template. Then click Add (Add) to open the Data Model creation guide.

Figure 1-4 Adding an Entity Data model to a project

  There are two options in the first Page wizard: Start with an existing database (created from an existing data); Start with an empty model (create an empty one). (the former option tag is actually "Generate from Database"), 1-5.

Figure 1-5 The Entity Data Model Wizard gives you the option to create and create an empty model from an existing database

creating a model from an existing database is a database first (Database-first) scenario. From the tables, views, and stored procedures of the database you select, the wizard creates a good model and an entity class for encoding. The immediate benefit is that you can use the Entity Framework in your encoding to map from tables and columns in the database to strongly typed entity classes in the model. If there is a relationship between the tables in the database, they will be modeled as associations. If you have a database that already exists, this is one way to create an Entity Data model for your app. However, if you prefer to use the code First (code-first) scenario to create a model from an existing database, don't worry, the Entity Framework development team has created a powerful toolset (the Entity Framework Power Tools), which, through reverse engineering, Create domain entity classes from an existing database, just like you do by hand.

If you are developing a brand new application and you have no existing database, you can also choose the Entity Framework Designer. Create an empty model first, and then right-click to create an entity type, association, inheritance. You can also create them from the toolbox by dragging them into the designer. Once the modeling is complete, you can right-click the designer to select the Generate database from Model menu item. It produces a script to create a database table, a relationship.

You can also create each entity class manually in Visual Studio, and then register them in the DbContext context object so that you can interface with the Entity Framework service. The Entity Framework maps These entity classes to the underlying database and, at run time, automatically creates a model in memory.

Model first (Model-first) or Database First (Database-first), modeled using the Entity Framework designer. One of the key points is shown in 1-6. In this model, customer and order have a one-to-many association, and each customer may have multiple orders, However, each order can only be associated with one customer. The Map Details window shows a customer mapping of an entity type to the Customer table in the underlying database. The window also shows that the columns in the Customer table are mapped to scalar attributes (scalar proerties) in the entity type Customer. Remember, in the Entity Framework, you can use data annotations or code precedence ( Code-first) in the API feature to create the same mapping.

Figure 1-6 Key points in designer modeling

Of course, the key points in designer modeling are far more than what is shown in Figure 1-6, and this book will cover all aspects of designer modeling. In some cases, beyond the capabilities of the designer, this book will show you how to handle such situations by modifying the underlying. edmx file. The file contains the complete model definition, including the conceptual layer, the storage logic layer, and the mapping layer.

So, regardless of whether we use Database First (Database-first), model first (Model-first), Code First (Code-first) in the Entity Framework, I eventually need to use a model . We have high productivity because we can use the Model (entity Class) as well as the normal objects in the application, as shown in 1-6, and in many cases you can use the customer and order objects just like you would with ordinary objects in your application.

If you want to insert a new customer and order into the database, you can create an instance of the customer and order type, setting their properties. It is then added to the in-memory context object that represents the model. Finally call the SaveChanges () method to save. The required SQL statement will be automatically generated and sent to the database to insert a row into the implementation. Get customers and orders from the database, you can create a query using LINQ or Entity SQL based on the entity types and associations in the model.

The techniques that run through this book will show you step-by-point how to model scenarios on almost every conceivable database, and how to query, insert, UPDATE, and delete through models. And how the Entity Framework is used in a variety of applications.

The first chapter, to this end. If you feel good, please click on the bottom right of the recommendation, in order to share with more people. Reprint Please specify source:http://www.cnblogs.com/VolcanoCloud/p/4478439.html

Entity Framework 6 Recipes Translation Series 2-----The first chapter to start using solid frame 2

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.