C# EntityFramework EF framework ModelFirst generates database methods from the model and shares additions, deletions, modifications and queries without handwriting SQL statements.


Preface

在c#中数据库方法需要手动写sql语句,比较麻烦,我最近在制作游戏时发现EF框架可以将操作数据库数据像访问List一样方便,于是自己研究了一下如何利用模型直接生成数据库

This article focuses on showing how to use models to generate databases and how to add, delete, modify, and query using SQL databases and SSMS database management tools. If there are any imprecision or even errors, please point them out in the comment area.


1. What is EntityFramework?

EntityFramework is an object mapping (ORM) technology developed by Microsoft. Using this framework, you can add, delete, modify, and query the database without writing any SQL statements.

2. Operation steps

1.Nuget package management

The demo project framework is .NetFramework4.7.2. If the .NetFramework framework is not used, it may cause an error that the target framework is not supported when creating the model.

NuGet Manager Search EntityFramework 

 

2. Edit the data model

Select Add->New Item in the blank space of the project browser and select ADO.NET Entity Data Model

Select empty EF designer model

 

Create an empty model, click on the model, right-click in the center of the screen to add an entity. In my understanding, the new entity can be regarded as a database table. 

Right-click the entity to add attributes, click the corresponding attribute, and adjust the name and value type in the attribute panel in the lower right corner.

Creating a new association allows two tables to establish a one-to-one, one-to-many, etc. relationship. 

After establishing the data table structure, right-click on the blank space to generate a database based on the model. 

Select the corresponding database. If there is no need to create a new connection, the method of connecting to the database is beyond the scope of this chapter. 

Click Next until completed. A security warning may pop up. Just confirm. 

After running, a file with the suffix sql will be generated. This is the sql statement. 

Drag the sql file into the database management tool and execute it. After the execution is completed and refreshed, you can see that the corresponding data table has been generated. 

 

 

3. Add, delete, modify and check functions

1. Add new data

//要访问数据库需要先实例化一个容器,实例用变量保存下来,只需实例化一次
Model1Container model1 = new Model1Container();

model1.UserSet.Add(new User() { Account = "123", Password = "123456" });
model1.UserSet.Add(new User() { Account = "321", Password = "654321" });
model1.SaveChanges();//保存修改到数据库

2. Delete data

Model1Container model1 = new Model1Container();

//删除UserSet表的所有元素
foreach (var user in model1.UserSet)
{
   model1.UserSet.Remove(user);
}

model1.SaveChanges();

3. Inquiry and modification

Model1Container model1 = new Model1Container();

//查询账号为123的用户
User user=model1.UserSet.Where(u=>u.Account=="123").FirstOrDefault();

if (user != null)
{
//修改用户123的密码为123456789
  user.Password = "123456789";
}
//保存修改到数据库
model1.SaveChanges();

4. Name problem

1. Container name, which is the name of the class that needs to be instantiated before use

 2. Data type name and database table name can be modified by clicking on the corresponding entity.

 After the name is modified, it needs to be regenerated, and the database also needs to be regenerated. Only the generated sql statement can take effect.


 

Summarize

The EF framework can make accessing the database as simple and clear as access lists, allowing people who do not understand SQL statements to easily access and modify the database.

This is my first time for a newbie to post a blog, please give it a like if it is useful.

The reason for blogging is:

1. Everything I learn comes from what others share.

2. Record your own learning process so that you can refer to it at any time later, and also want to see if your method is suitable.

Guess you like

Origin blog.csdn.net/m0_72922928/article/details/130807503