0

I have an Entity Class that is auto generated that I need to add some things do. I tried creating a new class and then trying to unbox and rebox it into my new class but that seemed like a lot of work.

Is there a better way to still take advantage of the Entity models but be able to add custom things to the classes?

public partial class SaveQuotes
{
    public int TableID {get; set;}    // Auto Generated
    public string Column1 {get; set;} // Auto Generated
    public string Column2 {get; set;} // Auto Generated

    // My custom additions needed
    public List<EdmundsVehicleInfo> EdmundsVehicleInfo {get; set;}
    public List<EdmundsPhoto> EdmundsPhoto {get; set;}

    // Auto Generated
    public virtual ContractTypes ContractTypes {get; set;}
}

Now if I update the model, my custom stuff is removed because it re-writes the file. I'm sure there is a way to add things to this class somewhere without it being re-written?

James Wilson
  • 5,074
  • 16
  • 63
  • 122

1 Answers1

4

This is a use case that was anticipated by the EF tooling.

Notice the use of the partial keyword in the class definition? You can make a second file with the same partial keyword and spread the class definition over two files.

The tools will only overwrite the original file, not the new one with your custom code in it.

If you want more info about partial classes, check this msdn article.

LoekD
  • 11,402
  • 17
  • 27
spender
  • 117,338
  • 33
  • 229
  • 351
  • I've been doing Entity for longer than I wish to admit not knowing about this....... – James Wilson Jul 19 '16 at 15:18
  • @JamesWilson It's a common paradigm for most autogenerated code in VS. Haven't done this for a while now... EF Code-first negates the need for this. – spender Jul 19 '16 at 15:22
  • We looked at Code-first but it seemed to give us trouble as the main database we use is a mirrored read only DB from a third party which seemed to trip up code first for us. – James Wilson Jul 19 '16 at 15:27