Repro:
class Program
{
static void Main(string[] args)
{
var test = new Test();
test.Run();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine("set breakpoint here -> Diagnostic Tools -> Take Snapshot -> View Heap -> context still rooted");
Console.ReadKey();
}
}
public class Test
{
public void Run()
{
using (var context = new Model1Container())
{
var sec1 = new Security { SysRowID = Guid.NewGuid(), Company = "Contoso", SecCode = "Foo" };
var sec2 = new Security { SysRowID = Guid.NewGuid(), Company = "Contoso", SecCode = "Bar" };
var sec3 = new Security { SysRowID = Guid.NewGuid(), Company = "Microsoft", SecCode = "Foo" };
context.AddToSecurities(sec1);
context.AddToSecurities(sec2);
context.AddToSecurities(sec3);
context.SaveChanges();
}
using (var context = new Model1Container())
{
Expression<Func<Model1Container, string, string, IEnumerable<Security>>> expression =
(ctx, company, token) =>
(from row in ctx.Securities
where row.Company == company && row.SecCode.StartsWith(token)
select row);
var query = CompiledQuery.Compile(expression);
var result = query(context, "Contoso", "Foo").ToList();
}
}
}
Model:
public partial class Model1Container : ObjectContext
{
public Model1Container() : base("name=Model1Container", "Model1Container")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
partial void OnContextCreated();
public ObjectSet<Security> Securities
{
get
{
if ((_Securities == null))
{
_Securities = base.CreateObjectSet<Security>("Securities");
}
return _Securities;
}
}
private ObjectSet<Security> _Securities;
public void AddToSecurities(Security security)
{
base.AddObject("Securities", security);
}
}
[EdmEntityTypeAttribute(NamespaceName="Model1", Name="Security")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Security : EntityObject
{
public static Security CreateSecurity(global::System.Guid sysRowID, global::System.String company, global::System.String secCode)
{
Security security = new Security();
security.SysRowID = sysRowID;
security.Company = company;
security.SecCode = secCode;
return security;
}
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Guid SysRowID
{
get
{
return _SysRowID;
}
set
{
if (_SysRowID != value)
{
OnSysRowIDChanging(value);
ReportPropertyChanging("SysRowID");
_SysRowID = StructuralObject.SetValidValue(value);
ReportPropertyChanged("SysRowID");
OnSysRowIDChanged();
}
}
}
private global::System.Guid _SysRowID;
partial void OnSysRowIDChanging(global::System.Guid value);
partial void OnSysRowIDChanged();
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String Company
{
get
{
return _Company;
}
set
{
OnCompanyChanging(value);
ReportPropertyChanging("Company");
_Company = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("Company");
OnCompanyChanged();
}
}
private global::System.String _Company;
partial void OnCompanyChanging(global::System.String value);
partial void OnCompanyChanged();
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String SecCode
{
get
{
return _SecCode;
}
set
{
OnSecCodeChanging(value);
ReportPropertyChanging("SecCode");
_SecCode = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("SecCode");
OnSecCodeChanged();
}
}
private global::System.String _SecCode;
partial void OnSecCodeChanging(global::System.String value);
partial void OnSecCodeChanged();
}