c# - Where should I connect to context for better performance? -


i want use ef , know 2 way use context accessing data methods of class:

1.passing connection each method of class:

 public partial class myentity  {     public static int add(myentityconnection context,myentity input)     {         context.myentity.addobject(input);         context.savechanges();         return input.id;     }  } 

2.using context on each method independently:

 public partial class myentity  {     public static int add(myentity input)     {         using (var context = new myentityconnection())         {             context.myentity.addobject(input);             context.savechanges();             return input.id;         }     }  } 

which of above or other ways better?

i'd recommend context per request per walther's comment, use dependency injection , repository pattern manage lifetime.

something like:

how-to inject entity framework dbcontext configurationbasedrepository of sharprepository


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -