-1

I have created an ASP.NET Core web application using the MVC pattern (by following this tutorial) and connected it to a local database that is now populated with some data. I have two questions, if someone please help me understand and answer them:

1) My default connection string is set to the following:

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MyAppName;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

Is it possible, only by changing the name and path in this connection string, to re-create the exact same database elsewhere with the data that is currently stored in it (for example, as backup)?

I have found the local database here:

C:\Users\my-name\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB

But when I try to copy the .mdf file to back up the database, I get an error that says the file is open and cannot be copied. What is it open in? How can I simply back the db file up?


2) After creating a custom controller, I noticed that all the provided properties of my Personmodel are used in the auto-generated code; e.g. in my case:

public async Task<IActionResult> Create([Bind("Birthday,ID,Username,EmailAddress")] PersonModel personModel)

I thought this would mean that if we change the code in the model class, we need to search for and effect the changes accordingly, but then I realized if I want to keep using auto-generated code, I have to do all the same steps as when I am generating it for the first time, and then when it asks if I want to replace the old code with new code, I choose yes.

Is there a better way of doing this, because this would overwrite my custom code every time and destroy the data stored in my local database. Particularly, when I store data in its local database, and then I decide to change a column name or add something, this would override everything...

How do I go about this situation?

1 Answer 1

3

Question 1

The .mdf and other files associated with the database will be in use by the SQL Server service. If you want to take a backup, use SQL Server Management Studio - right-click the database in the object explorer, select Tasks and then Backup. If you want to use the backup database then you need to restore it - again in the object explorer, right-click the "Databases" folder and select Restore Database and then browse to wherever you created the backup file.

Question 2 (updated 7th Sep)

When you change your model classes, you can use the add-migration command in the console to generate a new migration class containing the code to transform the database from its current structure to the new structure which matches the updated models. If the migration can cause data loss, then backup the database and restore the backup under a different name before running update-database, You can then create a script to transform the data from the backup into the new structure of the updated database.

Scaffolded components are a bit different to Entity Framework migrations. Migrations are truly auto-generated classes, and most of the time you wouldn't need to update (or even look at) the generated code. Think of scaffolded components as being more like a kind of template - it's a way of getting started with the classes, methods, markup etc that you're most likely to need, which is quicker than writing it all from scratch. It's not an alternative to writing code though, the intent is that once you've created the scaffolded code, you'll maintain it manually going forward. There is no way (that I know of) to automatically update scaffolded code to match a new model whilst retaining any edits you've made to it. You have two options

  • Re-scaffold the code and then apply your edits to it
  • Update the code manually to match the new model

All you can really do is weigh up the two options and decide which one is the least effort.

Sign up to request clarification or add additional context in comments.

8 Comments

Many thanks for your answer. The first one is clear. The second one - by auto-generated code, I mean the files created under the Migrations folder containing the code to create the database as well as all the other boilerplate code for the model controller and all the .cshtml files under the Views folder, etc...
My model has 20 properties, corresponding to database table columns, and there will certainly be changes to these properties, including removing some, renaming some, adding some, etc... I wonder if there is a way to simply modify my model class accordingly, and then update the rest of the code that uses those properties automatically, instead of searching for instances, manually renaming or deleting, etc...
I followed this tutorial to do the updating, but none of the mentioned commands actually work...
Ah, you created the view and controller using scaffolding as described here learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/… ? (Ignoring the fact that that tutorial uses a database-first approach and you're using a code-first approach). And you've since made your own edits to the view and controller and want a way of updating them to match the updated model without losing your edits?
Thanks for the clarification. I've updated the answer to make it more relevant to what you're trying to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.