简体   繁体   中英

How to add Extra properties to migration in .net core razor pages without deleting the previous migration or database in EF core

I am new in Asp.Net core and making a project Asp.Net core razor pages. So i am using EF core in my project for registration module I wanted to add extra fields of my own as well with the features provided by the EF core.so I created a User Class With additional field. and added the migration from package manager console. And Updated the database .But the problem is when I am adding 1 more property and trying to migrate its giving the error.so what I did is I deleted the migration from the migration folder and deleted the database also and generated the migration with new properties. but certainly this is not a good way of doing it plz can anyone suggest the proper way without deleting the database.

ApplicationUser.cs file

 public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }

}

Updating with Code First Approach takes little extra care wrt Database First Approach.

Initially, you must enable migrations for the project

Enable-Migrations -ContextTypeName DBNAME.StoreContext

This will create a migration folder with the Configuration.cs file. Next, we need to make our migration run. This is where you need to add an extra step to an existing database. If we create a migration now, it will attempt to add all our entities to the database. This will not work because other tables already exist in the database, so we need to create an initial blank migration and then later we will be able to add a migration for any new changes. To create blank migration execute this command

Add-Migration InitialCreate -IgnoreChanges

The key part of this command is the -IgnoreChanges flag, which ensures that migration is created that effectively does nothing. Running it will add an entry to the migrations table in the database, thus creating a snapshot of its original schema.

Next, run the update-database command to update the existing database with the initial migration. A new migrations table will now have been created in the DBNAME database.

Following this, you can add your properties, and after that execute this statement Add-Migration add_classname_propertyname

After that run, the update-database command and you are good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM