繁体   English   中英

在ObjectContext中组织渴望的查询

[英]Organizing Eager Queries in an ObjectContext

我正在弄混Entity Framework 3.5 SP1,并且试图找到一种更干净的方法来执行以下操作。

我有一个EF模型,我要添加一些“渴望加载”的实体,我希望它们全部位于上下文中的“渴望”属性中。 我们最初只是在更改实体集名称,但是仅使用属性并保持实体集名称完好无损。

例:

Context
 - EntityType
 - AnotherType
 - Eager (all of these would have .Includes to pull in all assoc. tables)
    - EntityType
    - AnotherType

目前,我正在使用合成,但是我觉得有一种更简单的方法可以完成我想要的事情。

namespace Entities{   
 public partial class TestObjectContext
{

   EagerExtensions Eager { get;set;}
   public TestObjectContext(){
     Eager = new EagerExtensions (this);
   }

}

 public partial class EagerExtensions 
 {
   TestObjectContext context;
   public EagerExtensions(TestObjectContext _context){
       context = _context;
   }
      public IQueryable<TestEntity> TestEntity
        {
            get
            {
                return context.TestEntity
                .Include("TestEntityType")
                .Include("Test.Attached.AttachedType")
                .AsQueryable();
            }
        }
 }
}



public class Tester{
  public void ShowHowIWantIt(){
     TestObjectContext context=  new TestObjectContext();
     var query = from a in context.Eager.TestEntity select a;

  }

}

使用扩展方法提供渴望的上下文实例? 优点是使依赖关系是单向的... TestObjectContext不依赖于EagerContext。

public namespace Entities.Eager
{
public static class EagerExtensions
{
  public static EagerContext AsEager(this TestObjectContext source)
  {
    return new EagerContext(source);
  }
}

public class EagerContext
{
  TestObjectContext _context;
  public EagerContext(TestObjectContext context)
  {
    _context = context;
  }

  public IQueryable<TestEntity> TestEntity
  {
    get{
      return _context.TestEntity.Include(....
    }
  }
}

}

和测试代码:

public class Tester
{
  public void ShowHowIWantIt()
  {
     TestObjectContext context =  new TestObjectContext();   
     var query = from a in context.AsEager().TestEntity select a;
  }   
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM