2

I am still new to EF and MVC and how they work together so excuse my ignorance. I have a SQL DB that I generated the MVC Data Model from and I have recently added some fields to a couple tables and now I need to update those changes from my SQL DB into my Model1.edmx.

The issue is that when I went to update the Model from my DB, the update overwrote all of my [Display(Name = "DisplayName")] and [Required] for each field.

So, my question is this: Is there a way to maintain all of my current changes to the Model and just bring in the new fields without overwriting every table in the Model?


Edit: I have tried this: Add data annotations to a class generated by entity framework but to no success. Whenever I regenerate the Model from the database it just overwrites whatever classes I had there. I am creating a public class just like in the example but it gets wiped when I update the Model. Any ideas?


Any help would be awesome! Thanks!

Alex
  • 175
  • 1
  • 3
  • 10
  • Could you provide the code for your controller? – xiience Aug 01 '17 at 15:34
  • @xiience Can you be more specific about what you need that for? I have 20 tables in the model and about 15 view models and then 12 controllers for the different views I have. I am just having trouble updating the model from the DB because it overwrites all of my changes. All the controllers are unchanged. – Alex Aug 01 '17 at 15:44
  • 2
    That's a common issue: https://stackoverflow.com/questions/16736494/add-data-annotations-to-a-class-generated-by-entity-framework – Steve Greene Aug 01 '17 at 15:54
  • @SteveGreene I will run with this. Looks like exactly what I need, thank you! – Alex Aug 01 '17 at 16:02
  • Sorry, I misread the question as Code-first DB and was confused about what was happening. Glad you figured it out! – xiience Aug 01 '17 at 17:16
  • 1
    Since you are new to MVC and EF, I think it's better you use ViewModels instead of your EF models on your client side. This will help you create your data annotations, prevent mass assignment and you won't need to bother when you updated your DB and EF models – Ziregbe Otee Aug 01 '17 at 17:32
  • @ZiregbeOtee So basically make all my tables in my Model1.edmx their own view model? That makes sense and then those can be routed back to the original .cs tables? – Alex Aug 01 '17 at 17:38
  • 2
    @Alex you'd need to assign the Entities(your current classes) to your Models. Usually done in a DTO, logic or business layer. The last code snippet here is an example of me retrieving data from my database layer and assigning the values into a view model, and you'd do the reverese when passing models up for an update. There are many different flavors of this approach, but generally it's pretty common that you don't pass your entity down to your Controllers. – AaronLS Aug 01 '17 at 17:48
  • 1
    Some good info [here](https://wildermuth.com/2015/07/22/Mapping_Between_Entities_and_View_Models) – Steve Greene Aug 01 '17 at 18:06
  • @AaronLS can you share that code snippet? So what I am hearing is that everything using `namespace projectname.Models` needs to have a corresponding ViewModel and the ViewModel is then used in the view to read/write data? Or am I still in left field? – Alex Aug 01 '17 at 18:19
  • @Alex Sorry I forgot to paste it. Bottom of this answer. This is just one way of doing it. `repo.` is a reference to a repository style data access layer, `.Get` returns your Entity type, and then you see the `new` creation of the view model and assigning properties from the entity to the view model. https://stackoverflow.com/a/45353240/84206 – AaronLS Aug 01 '17 at 18:21
  • @AaronLS okay I will take this and see how it fits into my situation. thank you for your help! – Alex Aug 01 '17 at 18:54

0 Answers0