gpt4 book ai didi

entity-framework-4 - 首先在 CTP4 代码中使用 CreateSourceQuery

转载 作者:行者123 更新时间:2023-12-04 08:00:25 25 4
gpt4 key购买 nike

我猜这是不可能的,但无论如何我都会把它扔在那里。在 CTP4 中使用 EF4 CodeFirst API 编程时是否可以使用 CreateSourceQuery?我想急切地加载附加到属性集合的属性,如下所示:

var sourceQuery = this.CurrentInvoice.PropertyInvoices.CreateSourceQuery();
sourceQuery.Include("Property").ToList();

但当然 CreateSourceQuery 是在 EntityCollection<T> 上定义的。 ,而 CodeFirst 使用普通的旧 ICollection (明显地)。有什么方法可以转换吗?

我已经完成了以下工作,但这并不是我想要的。任何人都知道如何从下面转到上面(下面的代码来自继承 DbContext 的类)?
ObjectSet<Person> OSPeople = base.ObjectContext.CreateObjectSet<Person>();
OSPeople.Include(Pinner => Pinner.Books).ToList();

谢谢!

编辑:这是我的 zeeshanhirani 发布的解决方案版本 - 顺便说一句,谁的书太棒了!
dynamic result;

if (invoice.PropertyInvoices is EntityCollection<PropertyInvoice>)
result = (invoices.PropertyInvoices as EntityCollection<PropertyInvoice>).CreateSourceQuery().Yadda.Yadda.Yadda
else
//must be a unit test!
result = invoices.PropertyInvoices;

return result.ToList();

编辑2:

好的,我刚刚意识到您不能在使用动态时分派(dispatch)扩展方法。所以我想我们不像 Ruby 那样动态,但是上面的例子很容易修改以适应这个限制

编辑3:

正如 zeeshanhirani 的博客文章中所提到的,这仅在(且仅当)您具有启用更改的代理时才有效,如果您的所有属性都被声明为虚拟,则该代理将被创建。这是将 CreateSourceQuery 与 POCO 结合使用的方法的另一个版本
public class Person {
public virtual int ID { get; set; }
public virtual string FName { get; set; }
public virtual string LName { get; set; }
public virtual double Weight { get; set; }
public virtual ICollection<Book> Books { get; set; }
}

public class Book {
public virtual int ID { get; set; }
public virtual string Title { get; set; }
public virtual int Pages { get; set; }
public virtual int OwnerID { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual Person Owner { get; set; }
}

public class Genre {
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual Genre ParentGenre { get; set; }
public virtual ICollection<Book> Books { get; set; }
}

public class BookContext : DbContext {
public void PrimeBooksCollectionToIncludeGenres(Person P) {
if (P.Books is EntityCollection<Book>)
(P.Books as EntityCollection<Book>).CreateSourceQuery().Include(b => b.Genres).ToList();
}

最佳答案

可以向派生上下文添加一个方法,该方法为实体实例上的给定导航创建源查询。为此,您需要使用底层 ObjectContext ,其中包括一个关系管理器,该管理器为每个导航公开底层实体集合/引用:

public ObjectQuery<T> CreateNavigationSourceQuery<T>(object entity, string navigationProperty)
{
var ose = this.ObjectContext.ObjectStateManager.GetObjectStateEntry(entity);
var rm = this.ObjectContext.ObjectStateManager.GetRelationshipManager(entity);

var entityType = (EntityType)ose.EntitySet.ElementType;
var navigation = entityType.NavigationProperties[navigationProperty];

var relatedEnd = rm.GetRelatedEnd(navigation.RelationshipType.FullName, navigation.ToEndMember.Name);

return ((dynamic)relatedEnd).CreateSourceQuery();
}

您可以花哨并接受导航属性的 Func 以避免必须指定 T,但以下是上述函数的使用方式:
using (var ctx = new ProductCatalog())
{
var food = ctx.Categories.Find("FOOD");
var foodsCount = ctx.CreateNavigationSourceQuery<Product>(food, "Products").Count();
}

希望这可以帮助!

〜罗文

关于entity-framework-4 - 首先在 CTP4 代码中使用 CreateSourceQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3398445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com