Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use DataBase First mode to add, delete, modify and check in Entity Framework

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use DataBase First mode to add, delete, modify and query in Entity Framework", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use DataBase First mode to add, delete, modify and search in Entity Framework.

Create a new console application, then right-click-> add a new item, and select the entity data model in the data:

Then click add

Select the EF designer from the database and click next

3. Select the data connection to be used in the entity data Model wizard interface, or click the New connection button to create a new connection. Select the existing connection here, and click next:

4. Select entity Framework 6.0, and click next:

5. Select the table to be operated and click finish:

6. View the generated project structure

A reference to EntityFramework is automatically added. A package folder is also generated under the root of the project:

In the package folder are files related to EntityFramework:

View the generated configuration file:

The name of a section segment in the ConfigSections node is entityFramework, and the name of a node below is the value of the section segment name. If you want to use EF configuration information elsewhere, you need to copy the configSections, connectionStrings, and entityFramework nodes, and the configSections node is at the front of all nodes.

7. View the generated edmx file 1. View the files under .tt

What is generated under the .tt file is the entity class corresponding to the database table, and it is in the form of buts.

2. View the .edmx file

Right-click on the edmx file to select the open method, and select the XML (text) editor to open:

The entire structure of the file is as follows:

Where SSDL content defines the structure of the database table:

CSDL content defines the structure of the entity class:

Cmurs mapping content defines the mapping between entity classes and database tables:

You can see from the Cmurs mapping content node why EF automatically generates entity classes, or why entity classes can manipulate data in the database.

From this you can see the first thing EF does: take the data table structure of the database, generate entity classes, and automatically establish a mapping relationship between tables and entities.

3. StudentManager.tt is a T4 template, which is used to generate entity classes.

T4 template: take the data structure from edmx and generate the entity class in the corresponding format according to the template.

4. The StudentManager.Context.cs file / /-/ this code has been generated from the template. / / manually changing this file may cause unexpected behavior in the application. / / if the code is regenerated, manual changes to this file will be overwritten. /-namespace EFDbFirstDemo {using System; using System.Data.Entity; using System.Data.Entity.Infrastructure Public partial class StudentSystemEntities: DbContext {public StudentSystemEntities (): base ("name=StudentSystemEntities") {} protected override void OnModelCreating (DbModelBuilder modelBuilder) {throw new UnintentionalCodeFirstException ();} public virtual DbSet Students {get; set;}

StudentSystemEntities inherits from DbContext and is used to manipulate the database.

At this point, I believe you have a deeper understanding of "how to use DataBase First mode to add, delete, modify and query in Entity Framework". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report