Controlling NHibernate entity Eagerloading - c#

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.

Related

Entity Framework Core: calling `Load()` after `Include()`

When requiring to eager load certain relations, I have noticed that in EF Core (v. 1.1.0) calling the Include method alone will not attach the required relation to the query, and when dealing with situation such as when the query must be passed to a view (ASP.Net MVC Core) where lazy loading will not work this causes problems.
However, I know that calling the Load method after the Include solves this problem. On the other hand, I also know that the Load is very similar to ToList as all the data available in a table will be loaded into the memory.
The point is that, I have faced situations where I have to call Load in order to be able to make a query based on properties available in a relation and I believe that this is a bad idea and using a Join would be more appropriate.
There are two questions:
1- Is calling Load really a bad idea in comparison with using a Join in order to attach navigational or relational properties?
2- What is the point of Include then if it does not include things on its own?

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?

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

Is there any utility or built in class in NHibernate that force a class to load it without lazy in a special situation?

I have an web application that uses NHibernate as ORM. I enable lazy loading for my classes but I want to load some classes without lazy in a special situation.
Now is there any utility or built in class in NHibernate that force a class to load it without lazy in a special situation?
Note: I don't want to remove lazy property in my mapping file, because I use lazy loading in some of time.
Yes. If you are using the ICriteria Api to specify your queries, you can use SetFetchmode to specify eager loading for some of the properties on a per-query basis.
I did a blog post on wrapping the behavior in a query object, it may be useful.
You might try looking at your system in different light.
http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan

Persistance Ignorance Linq to SQL

I have an existing domain layer. I want to develop the persistence layer using Linq to SQL. I am currently using an external map file. I am trying to use lazy loading for my child collections but am unsuccessful. Is there a way to implement lazy loading using Linq to SQL but without using EntitySet or EntityRef.
I can't guarantee that I'm up to the latest development of LTS, but previously you had to you use EntitySet/EntityRef to get lazy loading.
You're best bet is NHibernate if you want a PI-model.
(Not really answering the question.)
Entity Framework (aka. LINQ to Entities) in .NET 4 includes persistence ignorance support e.g. being able to map to POCO (Plain Old CLR Object1). See "Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0 ".
1 I.e. Not requiring a base class or attributes.

Categories

Resources