Ada*_*kis 7 entity-framework-4
我猜这是不可能的,但无论如何我会把它扔出去.在CTP4中使用EF4 CodeFirst API进行编程时是否可以使用CreateSourceQuery?我想热切地加载附加到属性集合的属性,如下所示:
var sourceQuery = this.CurrentInvoice.PropertyInvoices.CreateSourceQuery();
sourceQuery.Include("Property").ToList();
Run Code Online (Sandbox Code Playgroud)
但是当然定义了EntityCollection<T>CreateSourceQuery,而CodeFirst使用普通的ICollection(显然).有什么方法可以转换吗?
我已经得到了以下工作,但这并不是我想要的.任何人都知道如何从下面的内容到上面的内容(下面的代码来自一个继承DbContext的类)?
ObjectSet<Person> OSPeople = base.ObjectContext.CreateObjectSet<Person>();
OSPeople.Include(Pinner => Pinner.Books).ToList();
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:这是我的版本由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();
Run Code Online (Sandbox Code Playgroud)
EDIT2:
好吧,我刚刚意识到你在使用动态时无法调度扩展方法.所以我想我们是不是很为Ruby的动态,而上面的例子是易于修改这一限制相称
EDIT3:
正如zeeshanhirani的博客文章中所提到的,只有当(并且仅当)您具有启用更改的代理时才会起作用,如果您的所有属性都被声明为虚拟,则会创建代理.这是使用带有POCO的CreateSourceQuery的方法的另一个版本
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();
}
Run Code Online (Sandbox Code Playgroud)
小智 3
这样做绝对是可能的。如果您使用关键字标记了集合属性virtual,那么在运行时,您的实际具体类型ICollection将EntityCollection支持CreateSourceQuery默认代码生成器附带的所有好处。我将这样做。
public class Invoice
{
public virtual ICollection PropertyInvoices{get;set}
}
dynamic invoice = this.Invoice;
dynamic invoice = invoice.PropertyInvoices.CreateSourceQuery().Include("Property");
Run Code Online (Sandbox Code Playgroud)
我写了一篇关于类似内容的博客文章。ICollection请注意,依赖于转换为的内部实现并不是一个好的做法EntityCollection。以下是您可能会觉得有用的博客文章
| 归档时间: |
|
| 查看次数: |
2040 次 |
| 最近记录: |