6

I have an MVC 5 application that uses Entity Framework 6 Database First approach.

So far It is working well, but I have come across an unwanted behaviour.

If I select 'Update Model From Database', select the 'Add' Tab, and then select the Tables or Views I would like to add and click Finish, it adds the Tables and/or Views I specified no problem.

However, the unwanted behaviour is that, even though I didn't select the Refresh Tab, it seems every single model is automatically being refreshed.

This means all my custom Attributes on my models are being removed.

Is there any way to specify to only add the specified Tables or Views without refreshing all models, or if it refreshes all models, to retain the Attributes I have specified?

Visual Studio information: Microsoft Visual Studio Professional 2013 Version 12.0.40629.00 Update 5 Microsoft .NET Framework Version 4.5.51650

Installed Version: Professional

Is this a bug or intended use?

Thanks

Neill

Neill
  • 711
  • 2
  • 13
  • 32
  • How are you adding the attributes to your models? – Zippy Feb 05 '16 at 09:12
  • Manually typing them in on the model class. – Neill Feb 05 '16 at 09:15
  • 2
    @Neill, you shouldn't be adding them to generated model class as that can be regenerated each time you save the EDMX file. However, all the classes are partial classes so to add custom attributes you should create your own partial class which at compilation will be combined with the auto generated one. – Pheonyx Feb 05 '16 at 09:16
  • The only way partial classes help is as shown in the article linked on ARdC's answer. – Neill Feb 05 '16 at 14:30

2 Answers2

6

In order to modify autogenerated classes it's advised to use a partial class, that way your changes won't be lost when the class is refresh / generated again.

Simple example of a class and a partial class expanding on its attributes and methods

// Assume this is autogenerated by EntityFramework
public class Book {
    public int Id {get; set;}
    public string Title {get; set;}
}

// In a different file.cs
public partial class Book {
    [Required]
    public string Author {get; set;}

    public override ToString(){
        // Some code goes here
    }
}

In that example, if EntityFramework generates a new Book model, the changes you've done to that model via the partial class won't be lost at all.

Check out this article for more information on partial classes and this question for more info on the advantages of using partial classes.

Edit: if you need to add attributes to existing properties of your autogenerated class this answer might help you out as well.

Community
  • 1
  • 1
Alves RC
  • 1,778
  • 14
  • 26
  • Thanks ARdC. I indeed needed attributes on the auto generated classes, so the answer by Jean-François Beauchamp on the link you provided did the trick. – Neill Feb 05 '16 at 14:11
3

You really need to use partial classes so that you can refresh the edmx to your heart's content.

look here for a great tutorial on the subject.

rory
  • 1,490
  • 3
  • 22
  • 50
  • Downvoted because: 1. Yes, the link is for Database First, but points to information regarding "Enhancing Data Validation", not "Partial Classes". 2. I have seen the tutorial before, and reading through it again, I cannot see how it would solve the problem I have of using attributes on auto generated classes. That article would suffer from the same problem I have. – Neill Feb 05 '16 at 14:27
  • @Neill, are you blind? "Notice that each class is marked as a partial class, and each matches the name and namespace as the class that is automatically generated. By applying the metadata attribute to the partial class, you ensure that the data validation attributes will be applied to the automatically-generated class. These attributes will not be lost when you regenerate the model classes because the metadata attribute is applied in partial classes that are not regenerated." – rory Feb 05 '16 at 14:30
  • Took the time out to follow the tutorial. Nowhere does it talk about creating separate partial classes. It walks you through created a database, then connecting to it, hence the database first approach. And yes the attributes on the class do work; that is until you make any change to your database. And poof, any changes manually made to the file are gone. So while your statement to use partial classes is correct, the link you supplied, does NOT work out of the box. 20/20 vision here I'm afraid – Neill Feb 08 '16 at 07:16
  • @Neill - See the quote in my previous comment? Copy that onto your clipboard, open the link and press ctrl+f and then ctrl+v. Then tell me there's no match. Then get your eyes checked by a qualified person this time and consider reporting your current optic professional for malpractice and/or negligence. If they did it to you there might be countless other people out there just like you, who think they have 20/20 vision but are in fact serverly visually impaired – rory Feb 08 '16 at 13:09
  • 2
    I see no shame in admitting when I am wrong.... and boy was I wrong. Apologies rory, you are indeed correct. The lesson learned is never be so quick to try and prove somebody else wrong, you don't see the forest for the trees. – Neill Feb 09 '16 at 06:47
  • NOTE: I cannot upvote rory's answer: "Your vote is now locked in unless the answer is edited." – Neill Feb 09 '16 at 06:51
  • @Neill - answer edited. These things happen, don't sweat it – rory Feb 09 '16 at 07:19