cofoundry-cms / cofoundry

Cofoundry is an extensible and flexible .NET Core CMS & application framework focusing on code first development
https://www.cofoundry.org
MIT License
820 stars 144 forks source link

How to update existing data when data models are modified #263

Open dumboster opened 5 years ago

dumboster commented 5 years ago

Hello, I want to update data in DB when I modify the definition of data models : for example, if I rename a property in a data model of a content section, I don't want to loose data in this property. I saw that I could use Auto-Update, but I would like to know if it is the recommended solution. Thanks

HeyJoel commented 5 years ago

When you update a field in you c# model file the data will continue to exist in the database record until you update the entity, at which point the json data will be re-serialized and overwritten.

The best way to batch update your model data in the database will be to run a sql query using the built-in json functions in sqlserver, which are available in 2016+ or SqlAzure.

To run that script you can either do that manually or use the Cofoundry auto-update as you have suggested. There's an example of using the auto-updater in the SPA Site sample

I've pondered about whether we could make this easier by adding tools to help migrate model data, but any application-side helpers would suffer from poor performance with bulk oprations if large numbers of data models neede to be updated.

I'll leave this issue open and give it some thought further down the line:

dumboster commented 5 years ago

Thank you. Your response matches my thoughts.