when i create an entity framework datamodel automatically created MyModelEntities context.
i see some examples uses directly this class , some examples use the class in useing statement.
1.
public Employee GetEmployee(){
using (MyEntities ctx = new MyEntities())
{
return ctx.Employee.First(c => c.EmployeeID == intEmployeeID);
}
}
2.
MyEntities ctx = new MyEntities();
public Employee GetEmployee(){
return ctx.Employee.First(c => c.EmployeeID == intEmployeeID);
}
what is the difference
The "using" way will dispose of the myentities object when all the code within has executed. Refer: http://msdn.microsoft.com/en-us/library/yh598w02.aspx#Y38
The "using" way will dispose of the myentities object when all the code within has executed. Refer: http://msdn.microsoft.com/en-us/library/yh598w02.aspx#Y38
In Entity Framework ObjectContext implements the IDisposable interface. The rule of thumb is
If a class implements IDisposable then call its Dispose method when you are finished with it
Basically, the reason that the author of the class chose to implement IDisposable is because they want you to call Dispose when you are finished with objects of that class. Just do it. The easiest way to ensure that you always call Dispose when you are
finished with an object is to wrap the creation of the object in a using statement. This is what your first example does. Your first example is expanded by the compiler into this
MyEntities ctx = new MyEntities();
try {
return ctx.Employee....\\whatever
}
finally {
if (ctx != null)
((IDisposable)reader).Dispose();
}
This code ensures that the Dispose function is called whether or not an exception is thrown within the using block.
Your second example breaks the contract that the author of Entity Framework wants you to obey. The code does not call Dispose on ctx. Note that it would be wrong to call Dispose inside your GetEmployee() method because that method did not create the ctx
instance. Rather the code that creates the ctx instance is the one that should call Dispose (when it is finished). You can call Dispose however you like, it is just that the using statement is a convenience which makes it very easy to do so.
The garbage collector will dispose when the end line of the method is reached whereas if u use using it will
dispose all the references as soon as it comes out of the using statement
using is marginally faster then the other statement as you are telling the garbage collector to come into action manually
The garbage collector will dispose when the end line of the method is reached
I'm sorry, but this is just not correct. The object becomes eligible for garbage collection when the method ends but garbage collection itself runs when the CLR thinks it is a good time to run it. The decision the CLR makes takes many factors in
to account. The garbage collector may never run, if your program completes before gc kicks in, for example. The garbage collector may run in a few nanoseconds time or it may run tomorrow, it depends on the available memory, the amount of memory allocation
and the time since the last collection.
I agree with you partially but there is a method called GC.Collect. The
GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily
used for unique situations and testing.
youtubeline
Member
245 Points
256 Posts
using statement on linq and entity framework
Mar 20, 2011 12:20 PM|LINK
hi,
when i create an entity framework datamodel automatically created MyModelEntities context.
i see some examples uses directly this class , some examples use the class in useing statement.
1.
public Employee GetEmployee(){
using (MyEntities ctx = new MyEntities()) { return ctx.Employee.First(c => c.EmployeeID == intEmployeeID); } } 2. MyEntities ctx = new MyEntities(); public Employee GetEmployee(){ return ctx.Employee.First(c => c.EmployeeID == intEmployeeID); } what is the differenceMetalAsp.Net
All-Star
112085 Points
18242 Posts
Moderator
Re: using statement on linq and entity framework
Mar 20, 2011 12:28 PM|LINK
youtubeline
Member
245 Points
256 Posts
Re: using statement on linq and entity framework
Mar 20, 2011 01:25 PM|LINK
if i do not use when it will dispose?
which way is good?
MetalAsp.Net
All-Star
112085 Points
18242 Posts
Moderator
Re: using statement on linq and entity framework
Mar 20, 2011 03:42 PM|LINK
It will dispose based on when the garbage collector runs. The framework determines when that takes place.
youtubeline
Member
245 Points
256 Posts
Re: using statement on linq and entity framework
Mar 20, 2011 07:43 PM|LINK
which way is useful ?
MetalAsp.Net
All-Star
112085 Points
18242 Posts
Moderator
Re: using statement on linq and entity framework
Mar 20, 2011 09:47 PM|LINK
Paul Linton
Star
13407 Points
2533 Posts
Re: using statement on linq and entity framework
Mar 21, 2011 04:17 AM|LINK
In Entity Framework ObjectContext implements the IDisposable interface. The rule of thumb is
If a class implements IDisposable then call its Dispose method when you are finished with it
Basically, the reason that the author of the class chose to implement IDisposable is because they want you to call Dispose when you are finished with objects of that class. Just do it. The easiest way to ensure that you always call Dispose when you are finished with an object is to wrap the creation of the object in a using statement. This is what your first example does. Your first example is expanded by the compiler into this
MyEntities ctx = new MyEntities(); try { return ctx.Employee....\\whatever } finally { if (ctx != null) ((IDisposable)reader).Dispose(); }This code ensures that the Dispose function is called whether or not an exception is thrown within the using block.
Your second example breaks the contract that the author of Entity Framework wants you to obey. The code does not call Dispose on ctx. Note that it would be wrong to call Dispose inside your GetEmployee() method because that method did not create the ctx instance. Rather the code that creates the ctx instance is the one that should call Dispose (when it is finished). You can call Dispose however you like, it is just that the using statement is a convenience which makes it very easy to do so.
kratos_Vimal
Contributor
3744 Points
960 Posts
Re: using statement on linq and entity framework
Mar 21, 2011 04:41 AM|LINK
The garbage collector will dispose when the end line of the method is reached whereas if u use using it will
dispose all the references as soon as it comes out of the using statement
using is marginally faster then the other statement as you are telling the garbage collector to come into action manually
Vimal
Paul Linton
Star
13407 Points
2533 Posts
Re: using statement on linq and entity framework
Mar 21, 2011 06:08 AM|LINK
kratos_Vimal
Contributor
3744 Points
960 Posts
Re: using statement on linq and entity framework
Mar 21, 2011 06:59 AM|LINK
I agree with you partially but there is a method called GC.Collect. The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.
Vimal