by Someone1234 · 8y ago · 0 comments
EF definitely has the foreign key issue. We have around a thousand tables, we tried to generate the classes for all of them including foreign keys, problem is that when you create a context that references even only a single table, it will load everything that is foreign keyed including siblings of siblings of siblings, until you run out of memory.
Only way around it is to not set up foreign keys which massively diminishes the value of using EF, so we wound up creating two copies of each table's classes, one with and one without foreign keys. That causes its own issues.
This is a common theme I've seen with people blaming ORM's for being slow, it's the devs not using them appropriately more than the ORM's themselves. Not to say that they don't have their own issues.
LazyLoading impacts what data is retrieved from the database (or more to the point when), this is a structural issue before a query was even sent to the database. It would die while generating the query, not sending the query or populating the result.
You likely should have asked for more information before concluding it was "user error."
It impacts more than just that, it will impact the query generation as well. If you have a property that is not lazy loaded then it will attempt to join the relation or load it very another query in the same round trip. Turning it off tells the ORM that every single time you want A it needs to go and get B as well. If you have it off universally it will attempt to load the entire database, or as may be the case here, crashing while trying to generate a query to do so.
> You likely should have asked for more information before concluding it was "user error."
Perhaps, but you've got multiple conflicting accounts of what went wrong, some comments indicate that it returned data, others say it never touched the database.
What I have heard of is traversing the entire graph and causing cascading loading of navigational properties. Yes, I have done that. Something like AutoMapper will do that to you, if you are not careful. Been there and done that.
How did you determine that it OOMed while generating the query?
By looking at the call stack when the exception was thrown (and the fact that nothing hit the database).
As someone else suggested, you probably had lazy loading enabled, and some of your code tried - e.g. through reflection - to get all properties.
EF has some of it's own issues - but you can most certainly create composite primary keys, composite foreign keys and work with projections right from within the code.
None of the issues the original author had with SQLAcademy and Hibernate are really a pain in EF.
Some prefer to switch off lazy loading in EF and instead either explicit eagerly load specific child collections or explicitly load them right before use.
In EF you can do
var customers = Customers.Include(c => c.Orders).Single(c => c.CustomerNo == '1234')
This will load Customer '1234' with the Orders collection eagerly loaded.