C
C#12mo ago
TYoemtais.z

❔ Replace Include / ThenInclude with separate calls for entities

Let's assume a simple case. I have a class Fleet with properties Id, Name, and Cars as List<Car>. When I do context.Fleets.FirstOrDefault(x => x.Id == 1).Include(x => x.Cars) and then remove a car from the list and call SaveChanges, EF knows that this entity no longer has a connection with the given Car and removes the connection. But what happens if I do the following: var fleet = context.Fleets.FirstOrDefault(x => x.Id == 1); var cars = context.Cars.Where(x => x.FleetId == 1).ToList(); fleet. Carss = cars; What will happen if I now remove an entity from this list and save the changes? Will it be the same as if I had used Include? If not, is it possible to make it work in a similar way? For performance reasons, I need to remove all Include/ThenInclude and pull entities separately. However, managing entities in lists in the main entity is convenient, and it would be nice if it could be done in a similar way
9 Replies
jiniux
jiniux12mo ago
what are the performance issues that you are experiencing?
TYoemtais.z
TYoemtais.z12mo ago
When I use 6 "include" statements and some "thenInclude" statements because I need many related entities, the generated SQL returns 0,5 milion rows to me, and it takes 15 seconds. However, when I do it separately, even splitting it into 3 calls, it takes only 300 milliseconds and returns a few hundred rows.
Angius
Angius12mo ago
Why not use ExecuteUpdateAsync() and simply set the foreign key to null, instead of pulling the entire database into memory?
TYoemtais.z
TYoemtais.z12mo ago
Im just asking if it is possible to do so, as I said, managing entities in lists in the main entity is convenient, and it would be nice if it could be done in a similar way. Also, I've already have logic for that, and don't like to change it. I use soft-delete pattern almost everywhere so I need to load these entities. I also was thinking that I maybe can change state of entities to Deleted, so the interceptor will change IsDeleted to true.
D.Mentia
D.Mentia12mo ago
You should generally be operating on a few cars from the db at a time, probably just one, not an entire fleet. And then just .Include(x => x.Fleet) on each Car that you query, instead of including all the cars in a query on the fleet You can then assign a new Fleet to the Car, which will move it from the old to the new, or assign a null Fleet on the Car (or a null FleetId), or assign a new FleetId to the Car, or remove it from the Fleet's list if you still really want that Or of course, just .Remove the Car if you're trying to delete them But no, change-tracking won't work with your example, unless the Car actually contains a Fleet or the Fleet actually contains Cars
TYoemtais.z
TYoemtais.z12mo ago
Assuming that each car has some set of tags like "new", "VIP" and so on, another set of items like a list of log entries, a list of allowed drivers, a list of customers and so on. Then from the front end comes the entire updated fleet, where in each car all the above lists could be changed. I can't change the front end because that's the way it already is. Should I then query from db each car separately and edit it? Will dozens of small queries in a loop be good?
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
TYoemtais.z
TYoemtais.z12mo ago
I would be incredibly grateful for small advice.
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.