开发者

LINQ no delayed loading

开发者 https://www.devze.com 2023-02-10 11:53 出处:网络
Is there an easy开发者_如何学运维 way to force linq to execute a join on a foreign key relationship without accessing an arbitrary member of the referenced table?

Is there an easy开发者_如何学运维 way to force linq to execute a join on a foreign key relationship without accessing an arbitrary member of the referenced table?

For example, if I have a Company object that references a TaxBracket table and there is a relationship and linq association set up between them I want to be able to access Company.TaxBracket after the data context has been disposed and without having to do var temp = Company.TaxBracket.randomMemberVariableToExecuteQuery


You can use DataLoadOptions

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Company>(c => c.TaxBracket);
using(var context = new DBContext(){LoadOptions = options})
{
    //...
}


Use DataLoadOptions.LoadWith. So, you'd say something like

using(var context = new MyDataContext()) {
    DataLoadOptions dataLoadOptions = new DataLoadOptions();
    dataLoadOptions.LoadWith<Company>(c => c.TaxBracket);
    context.LoadOptions = dataLoadOptions;

    // some query
}
0

精彩评论

暂无评论...
验证码 换一张
取 消