Hi,
I have the follwing method.
public List<MyEntity> GetMyEntities(MyObjectContext objCtx)
{
using(MyObjectContext ctx = objCtx ?? new MyObjectContext())
{
retun ctx.MyEntities.ToList();
}
}
The idea is, user of this method can pass in the objectcontext if they have. If not then a new objectcontext will be created.
If I am passing an object context to it, then it is getting disposed after the method is done. I was expecting only "ctx" variable gets disposed.
If I write a small app, to know the using and dispose mechanism. It is acting differently.
class TestClass : IDisposable
{
public int Number
{
get;
set;
}
public string Str
{
get;
set;
}
public ChildClass Child
{
get;
set;
}
#region IDisposable Members
public void Dispose()
{
Console.WriteLine("Disposed is called");
}
#endregion
}
class ChildClass : IDisposable
{
public string StrChild
{
get;
set;
}
#region IDisposable Members
public void Dispose()
{
Console.WriteLine("Child Disposed is called");
}
#endregion
}
class Program
{
static void Main(string[] args)
{
TestClass test = null;
test = new TestClass();
test.Child = new ChildClass();
using (TestClass test1 = test ?? new TestClass())
{
test1.Number = 1;
test1.Str = "hi";
test1.Child.StrChild = "Child one";
test1.Child.Dispose();
}
test.Str = "hi";
test.Child.StrChild = "hi child";
Console.ReadLine();
}
}
In this example, "test1"gets disposed but not "test". Where as in the first case both ctx and objCtx get disposed.
Any ideas what is happening here with objectContext?
Thank you, Suresh