Entity Framework core 2.1, add seed data

Source: Internet
Author: User

Efcore 2.1 has been released for a while, and its new functions are not used yet. Today I will study how to use EF core 2.1 to add seed data.

This part of the official documentation address is: https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding

We always need to add some seed data during development, so this function is quite useful.

 

Preparations

I have created an ASP. NET core project with several models, one of which is the province and the other is the city:

 

There are other models involved in it, but if this document is not used, it will not be pasted.

 

The database used by this project isMSSQL localdb. The above models migration has been completed.

Some data exists in the database, but I have deleted it now.

 

Add the first seed data

Use the onmodelcreating method of dbcontext directly.Hasdata ()Method:

Here I added the seed data of a province andWrite the value of the primary key ID.

The primary key ID of the database table is int auto-incrementing. Data with ID 1 already exists, but is deleted.

Then let's see what will happen.

 

Generated migration class

Command: Add-migration xxx

Take a look at the generated migration class content:

 

Generated SQL script

Command: script-migration

This section describes how to insert data:

 

Migrate to database

Command: Update-database-verbose

The result is successful.

According to the red lines, efcore temporarily changed the settings during execution, and can insert the value of the primary key, and then disable the insert primary key.

 

Data in the database

Although data with ID 1 has been deleted, the seed data with ID 1 can still be inserted.

 

The primary key of the seed data must have a value.

I will add a seed data with no primary key ID value:

 

Then add-migration to see what will happen:

An error is reported, soThe primary key value is required.

 

After I enter the primary key value, everything is easy to use:

 

Change existing seed data

I changed the existing seed data in the hasdata method, but the primary key value is not changed:

SQL statement for executing Update-database:

We can see that the data in the database is updated based on the primary key.

 

The result is also the same as I thought, that is, the existing data is updated:

 

If I modify the primary key value of the seed data in hasdata

I changed the primary key of Sichuan from 2 to 3.

 

See the generated migration file:

First, the added seed data with ID 2 is deleted, and a data with ID 3 is inserted.

 

Check the SQL:

Delete first and then insert.

 

In the database:

 

Why Do seed data need to specify the primary key value?

This ensures that different developers, computers, and servers have the same seed data in the same migration version..

 

Add associated seed data

There is a one-to-many relationship between province and city. That is to say, a province can have multiple cities and there are navigation attributes between them.

Let's see if you can add province and city at one time. I will write it like this in the hasdata method:

 

Then add-migration

This cannot be done. MeThe seed data of city must be added separately and the foreign key must be set..

 

So the correct method is:

No error is reported for this add-migration operation, and the migration is successful. Let's take a look at the final data:

OK

 

If the primary key/foreign key cannot be set in the Model

Sometimes, the foreign key is not clearly defined in the model of the master-slave relationship; sometimes the primary key of the model is private set;

In this case, we cannot set the primary key/foreign key value in hasdata. How can we add the seed data?

The answer is to useAnonymous class.

 

I removed the foreign key from the city model (the navigation property is retained, and the master-slave relationship with province still exists ):

 

Then you can add the seed data as follows:

 

Data after migration:

The results are still as expected.

 

What if the primary key is of the guid type?

Check the data:

It seems that there is no problem.

 

What if I do not modify the seed data and perform another migration?

Take a look at the Migration file:

Delete the original data and insert a new data ..

The same is true for databases:

 

So the best way isPut the guid value in a variable.:

Then perform the following operations:

In this way, the "delete original data and re-insert" operation will not occur.

 

Others

Use context. database.Ensurecreated ()A new database is created and contains seed data. However, if the database already exists, ensurecreated () will not update the database or add seed data.

 

Entity Framework core 2.1, add seed data

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.