简体   繁体   中英

How to update models in asp.net through database first approach while keeping some previous methods alive

I have the following columns in the Patients table:

Patients 表的屏幕截图

I've created models using the following command.

Scaffold-DbContext "Server=.;Database=Tasks3;Trusted_Connection=True;" 
         Microsoft.EntityFrameworkCore.SqlServer 
         -OutputDir Models\MainModel

Screenshot of Patient Model

I've some methods in context class to make my connection string dynamic. Actually I used to fetch data from tokens and after some logic connection string changes from client to client.

Now the problem is here, when I make changes in patients table (for instance, I changed CNIC column from CNIC5 to CNIC ) and run the following command with -Force keyword, it delete all the data from the previous Tasks3Context class ( DbContext ).

Scaffold-DbContext "Server=.;Database=Tasks3;Trusted_Connection=True;" 
         Microsoft.EntityFrameworkCore.SqlServer 
         -OutputDir Models\MainModel -t <Patient> -f

Tell me some method that update models and make changes in only specific model and column.

Thanks in advance!

I tried the database update procedure, and when I used -Force for the second time, there was a problem similar to yours. The specific reasons are as follows:

The code generated by EF Core is your code. Feel free to change it. It will only be regenerated if you reverse engineer the same model again. The scaffolded code represents one model that can be used to access the database, but it's certainly not the only model that can be used.

Customize the entity type classes and DbContext class to fit your needs. For example, you may choose to rename types and properties, introduce inheritance hierarchies, or split a table into multiple entities. You can also remove non-unique indexes, unused sequences and navigation properties, optional scalar properties, and constraint names from the model.

You can also add additional constructors, methods, properties, etc. using another partial class in a separate file. This approach works even when you intend to reverse engineer the model again.

After making changes to the database, you may need to update your EF Core model to reflect those changes. If the database changes are simple, it may be easiest just to manually make the changes to your EF Core model. For example, renaming a table or column, removing a column, or updating a column's type are trivial changes to make in code.

More significant changes, however, are not as easy to make manually. One common workflow is to reverse engineer the model from the database again using -Force (PMC) or --force (CLI) to overwrite the existing model with an updated one.

Another commonly requested feature is the ability to update the model from the database while preserving customization like renames, type hierarchies, etc. Use issue this to track the progress of this feature.

Warning

If you reverse engineer the model from the database again, any changes you've made to the files will be lost.

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