c# - How to preserve the Model.Context.cs file header content after updating model from db with Entity Framework? -


the model.context.cs file automatically generated when using entity framework database first.

i have modified header part of class compatible unit of work , repository pattern:

public partial class mydatabasecontext : dbcontext, idbcontext {     public mydatabasecontext()         : base("name=mydatabasecontextentities")     {         // disable lazy loading best practices - keeps accidentaly loading large entity graphs         configuration.lazyloadingenabled = false;     }      // refactored     public new idbset<t> set<t>() t : class     {         return base.set<t>();     }      public override int savechanges()     {         // this.applystatechanges();         return base.savechanges();     }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         throw new unintentionalcodefirstexception();     }     .     .     . 

the issue whenever update model db, obviously, file gets overwritten.

i've saved relevant code in commented out portion of *.config, , copy/paste in place whenever model updated.

even though model not updated frequently, i'd know if there way of preserving content don't have go through step of copy/pasting.

for added members, can create file contains partial of class. new file won't overwritten you've added there should persist between updates.

for changes aren't new members (like added line in constructor), can edit model.context.tt file directly. simple t4 text template used re-generate code when model updated. similar asp.net page; however, instead of using c# generate html, uses c# generate more c#.


Comments