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

What is the data model and data migration of EF Code First in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What this article shares with you is about the data model and data migration of EF Code First in C#. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I. the creation of EF

Step 1: create a class library

Step 2: select a class library

Step 3: select the ADO.NET entity data model. The name corresponds to your database name. Mine is called LetDB.

Step 4: the form appears

Explain these EF models first:

1. EF designer from the database:

First there is the database, and the model is generated according to the database.

two。 Empty EF design model:

Model first, generate database through the designed model

3. Empty Code First model:

Code first, generating database through code

4. Code First from the database:

Generate entities according to the database, and you can modify the database through code

Step 5: select the fourth model

Connect to the database:

Click New connection:

Just choose!

Objects contained in the model: select the table, see if the view has it, and add it if necessary.

Click finish to create!

The creation is like this: the table of your database becomes an entity class, and the class has fields of the table.

LetDB displays managed tables, which are not explained in detail

Second, modify the database 1. Add database fields

What do we need to do if we want to add a field?

Step 1: find the Manager console

The following deepens the code. The fields you want to add can be typed in the corresponding table cs first.

Console input: (start database migration)

Enable-Migrations press enter

The field Sstate you want to add

The following occurs:

Step 1: a folder and a class appear

Step 2: go in and change the attribute to true

Step 3: delete the database (don't run away) I strongly recommend that you try a useless database first and you are not responsible for the problem!

Step 4: generate the database through code

Update-database

If you succeed, you will find that the database is back, there is an extra table, and the fields you want to add are here, so why a new table?

What does this table do: it is the table structure of the system that stores the information and version of your database, and determines whether to update the database according to the version, so the reason for deleting the database is to generate such a thing.

Second, add database tables

Step 1: copy a class that you already have

Then change the name: change the name after class and the name of the constructor

Note that this should also be deleted.

Add two fields:

After doing the above, we also need to go to the table model to bind the newly added table.

That's my name. Anyway, it's the cs class with the same name as your database.

Bind:

Then the console:

Update-database

Refresh the database and you can see the newly added table

The above is the basic use of EF, if it helps you, please give me a like! There are inaccuracies to be pointed out in the comments section, ah, there are not clear how to use can be commented on, can be private, will be solved.

The above is the data model and data migration of EF Code First in C#. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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