Get EF6 Proxy for POCO Entity - c#

So I have code which has created a poco entity (from deserializing JSON). I wish to enable lazy loading for the the entity, so I want Entity Framework to wrap my POCO object and give me an EF proxy that can lazy load navigation properties.
I've seen many answers for questions regarding getting the underlying poco entity from an EF proxy, but not the other way around given. Given a poco entity how do you create an EF proxy wrapping it?
Both...
context.Set<TEntity>().Add(poco);
and...
context.Set<TEntity>().Attach(poco);
...return the poco entity is there a method somewhere that will give me a proxy for the poco?
I'm able to get proxied entities and lazy loading is working fine when I load an entity through...
context.Set<TEntity>().Where(...)

You can't lazy load navigation properties unless you are querying from the context. If you are loading the entity by the deserialization of JSON you need to use explicit loading.
//Example
context.Entry(poco).Reference(x => x.ReferenceProperty).Load();
context.Entry(poco).Collection(x => x.ReferenceCollection).Load();
https://msdn.microsoft.com/en-us/data/jj574232.aspx

Related

AutoMapper binding issues with Entity Framework mapping

I'm using AutoMapper as an ORM in application with Entity Framework v6.
My problem statement is that if I executes a single query via LINQ to Entities on any single entity and maps entity data via AutoMapper with DTO, many other queries will be executed at the same time while data binding.
Kindly anyone let me know, how to avoid this problem.
This is expected behavior. Remember, AutoMapper just looks at the destination type and will map matching properties from the source type. If you don't want some properties to be mapped lazily, you have several options:
Remove the properties from the destination model
Use Include to eagerly fetch properties
Use LINQ projection in AutoMapper (ProjectTo) to force EF to use the Select LINQ projection to grab whatever data it needs to based on the source type
I think this may happens due to the lazy loading enabled.
Read this answer :
Is automapper preventing lazy loading with EF?

Controlling NHibernate entity Eagerloading

I'm relatively new to NHibernate. I'm working on a web app in which i need to detach the entity from Hibernate session and pass it to UI for changes. I'm using NHibernateUtil.Initialize for eager loading of the entity. The problem i'm facing is the entity has a lot of collections and some collections have even child collections, so when i'm using NHibernateUtil.Initialize it is eager loading all the collections, which in turn increasing my JSON data that i pass to UI layer.I'm using ASP.NET Webapi in the service layer. Is there a way to control the eager loading to only particular collections that i need in the entity . Any help would be appreciated
Thanks
In your mapping you can specify the default behaviour for lazy loading of collections and references. How are you creating you mappings? You can override the default by specifying a fetch strategy in individual queries. What module are you using for your query (QueryOver, Linq etc.)? My preference is to always set the default to lazy load and then eager fetch as required.
Also, my preference is to not pass the entity directly to the UI layer but to create a ViewModel in the Controller.

Entity Framework 6: Lazy loading doesn't work when classes are internal

I'm using Entity Framework 6 and I have lazy loading enabled:
When I set my entity class Access to 'public, lazy loading works fine:
In the example above, I'm able to navigate from Address to City.
However, if I change the entity class Access to 'internal', then lazy loading stops working and I can no longer navigate from Address to City if I don't eager load City:
Why does lazy loading stop working for internal classes? Is there a way around this?
The entity must be public, it's a requirement for Entity Framework to be able to inherit from it and create a proxy at runtime (that adds all the EF internal stuff in the overridden virtual navigation properties).
See Requirements for Creating POCO Proxies on MSDN.

POCO/PROXY Disabling Lazy Loading

In the book "Entity Framework 4 in Action", in section 16.1.3 and again in 16.2.4, it is stated
If you have a proxy instance, remember to disable lazy loading before
serializing, or you’ll end up sending unwanted data to the client.
However, the authors never say how to disable lazy loading. In my own searches, I've heard of ContextOptions.LazyLoadingEnabled. Is that "the way" this is done? Is there more than one way (besides disabling proxy generation)?
I read in one article that the LazyLoadingEnabled flag only pertains to EntityObject entities, not POCO entites:
Turning lazy loading off will now allow your classes to be serialized
properly. Note, this is if you are using the standard Entity
Framework classes. If you are using POCO, you will have to do
something slightly different.
With POCO, the Entity Framework will create proxy classes by default
that allow things like lazy loading to work with POCO. This proxy
basically creates a proxy object that is a full Entity Framework
object that sits between the context and the POCO object. When using
POCO with WCF (or any serialization) just turning off lazy loading
doesn’t cut it. You have to turn off the proxy creation to ensure
that your classes will serialize properly
I suspect the above commentary is simply erroneous.
Yes, ContextOptions.LazyLoadingEnabled is "the way." The serialization issue is related, but different. Proxies have a different runtime type. That can mung serialization. The only relation to lazy loading is that pure (non-proxy) POCOs don't do lazy loading.

Entity framework auto eager load

Is there a way to get entity objects to automatically pull all relevant data through the relationships instead of having having to .Include everything I want populated? I can't use lazy loading as this needs to be serialized to go over WCF. It would just be fantastic to have it auto populate the relevant collections.
No there is no build in feature which will automatically eagear load whole object graph. You must always explicitly say which navigation properties you want to eager load (by using Include method or LoadProperty method).
Edit:
Based on your comment: Generally it should be possible to build some auto loader - but it will not be easy. ObjectContext has MetadataWorkspace property which contains all metadata about your entities. You can load all information about your entities from metadata and add needed inclueds to the query. I expect one problem - you must somehow handle cyclic references. If you need some example how to extract information about entities check T4 POCO generation template.
I came across this querstion recently because I was looking for something similar.
Since EF Core 6 there is a AutoInclude method that configures whether a navigation should be included automatically.
This can be done in the OnModelCreation method in the DbContext class:
modelBuilder.Entity<Theme>().Navigation(e => e.ColorScheme).AutoInclude();
This would load the ColorScheme for every Theme when running the query:
var themes = context.Themes.ToList();
See Model configuration for auto-including navigations

Categories

Resources