I suppose this is not possible, but I will throw it away anyway. Can I use CreateSourceQuery when programming with the EF4 CodeFirst API in CTP4? I would like to download properties related to a set of properties, for example:
var sourceQuery = this.CurrentInvoice.PropertyInvoices.CreateSourceQuery(); sourceQuery.Include("Property").ToList();
But of course, CreateSourceQuery is defined in EntityCollection<T> , while CodeFirst uses a plain old ICollection (obviously). Is there any way to convert?
I got below to work, but this is not quite what I am looking for. Does anyone know how to move from what is lower to what is higher (the code below refers to the class that inherits DbContext)?
ObjectSet<Person> OSPeople = base.ObjectContext.CreateObjectSet<Person>(); OSPeople.Include(Pinner => Pinner.Books).ToList();
Thanks!
EDIT: here is my version of the solution sent by zeeshanhirani - who, incidentally, is studying great!
dynamic result; if (invoice.PropertyInvoices is EntityCollection<PropertyInvoice>) result = (invoices.PropertyInvoices as EntityCollection<PropertyInvoice>).CreateSourceQuery().Yadda.Yadda.Yadda else
EDIT2:
Ok, I just realized that you cannot send extension methods when using dynamic. Therefore, I think that we are not as dynamic as Ruby, but the above example is easily modified to meet this limitation.
EDIT3:
As mentioned in a blog post by zeeshanhirani, this only works if (and only if) you have shift-enabled permissions that will be created if all of your properties are declared virtual. Here is another version of how a method might look like using CreateSourceQuery with POCOs
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(); }
entity-framework-4
Adam rackis
source share