Changes to the database with Asp.net MVC 5

Asked

Viewed 496 times

5

I am starting some independent projects using Asp.net MVC 5 with Entity Framework, and more learning from the technology. And at the moment a great doubt arose.

I used the code generated through a connection to the database, creating an ADO.NET Entity Data Model (edmx). And it creates the object classes of the database everything. However, I needed to make some changes to the database, such as adding more tables, removing some columns and Foreing Key and adding others.

Now what? What about my code, outdated? Since I already created Controller, and views. There is how to update, or I have to make the changes all in hand?

  • 3

    Therefore the project should be well structured with minimal changes or increasing changes, in the specific case, update the model (EF) and then the views manually or delete and generate again your views. Changes always occur this is normal in the projects until a certain point as it is learning search more information in the site http://www.asp.net/ in the MVC part. Good Luck.

  • 1

    Thank you @Maria, and just as I’m learning and going in search of finding the best way ever, I got to that point, and I didn’t know what to do, if I spawned everything again, or updated it manually, so I decided to ask and research first of all, because my project is still in the beginning, but when it needed a future change, when it was already developed enough things. I believe it would not be feasible to delete and generate everything again.

  • 1

    I for example @Ericosouza, only update and mute manually, because they are usually details... well I think... rs!

  • Oh yes @Maria, well in my case at first seeing would also be only details, would see no harm in changing in the hand, but then I came across the thought "Oops, what if I had to change more things? And if it’s a big project and tomorrow my client wants a new functionality...", It’s okay that there would be no harm in changing in the hand, but before making any decision, I thought it best to research and ask to know the best way.

1 answer

4


What now? What about my code, outdated? Since I have already created Controller, and views.

Exactly, more at the level of Views than of Controllers, but the Controllers may be affected if there is a load of data from dependent entities such as Dropdowns mounted in the Controller, for example.

There is how to update, or I have to make the changes all in hand?

It does, but this way can be a bit drastic, which is using Scaffolding, that is, allowing Visual Studio to generate this back for you. In this case, your files may be overwritten. Before trying Scaffolding, recommend making a backup of your project.

There are two ways to do this:

  • Right-clicking on the directory Controllers > Add > Controller...
  • Using a Nuget package like Mvcscaffolding.

By this method, Controllers and Views may be generated from your Model.

To learn more, I recommend reading:

  • Thank you @Ciganomorrisonmendez

  • and if I use the "Update model from database" in my edmx, I will update the models, correct? From there I won’t have "many" problems generating the new controller... right?

  • 2

    Yes, the generation step of Controllers is trivial. Just be careful because any logic implemented by you that is not the standard of Scaffolders can be lost.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.