我正在搞乱Entity Framework 3.5 SP1,我正试图找到一种更简洁的方法来执行下面的操作。
我有一个EF模型,我正在添加一些Eager Loaded实体,我希望它们都驻留在上下文中的“Eager”属性中。我们最初只是更改实体集名称,但是使用属性似乎更清晰,并保持实体集名称。
示例:
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;
}
}
答案 0 :(得分:1)
使用扩展方法提供急切的上下文实例?优点是使依赖单向... 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;
}
}