Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Which version?


We are currently on the latest.

We had an issue last week where we had an obect like

    public class Foo
    {
        public List<Bar> Bars { get; set; }
    }
We'd query for some Foos, like:

    await _dbContext.Foos.ToListAsync();
and some amount of them would have Bars be an empty list where it should definitely be populated from the db. And it wasn't even consistent, sometimes it would populate thousands, sometimes it would populate a handful and then just stop populating Bars.

No errors, no exceptions, just empty lists where we'd expect data.

And so often we have to debug and see what SQL its actually generating, then spend time trying to get it to generate reasonable sql, when if we were using sprocs we could just write the damn sql quicker.

Another issue we have is the _EFMIgratoinsHistory table.

Sometimes we will deploy and get a load of migration errors, as it tries to run migrations its already ran... SO they all fail and then the API doesn't come back up... The fix ? TUrn it off and on again and it stops trying to re-run migrations its already ran!


navigation properties are not loaded automatically, because they can be expensive. you need to use `.Include(foo => foo.Bars)` to tell EF to retrieve them.

EF tries to be smart and will fix up the property in memory if the referenced entities are returned in separate queries. but if those queries don't return all records in `Foo.Bar`, `Foo.Bar` will only be partially populated.

this can be confusing and is one of the reasons i almost never use navigation properties when working with EF.


We have those, and when I say inconsistent I mean inconsistent on the same query / exact same line of code on the same database.

e.g. stick a breakpoint, step over, see in the debugger that it was not populating everything it should. Then run it again, do the same and see different results. Exact same code, exact same db, different results.

5000 results back from the db, anything between 5000 and a handful were only fully correctly populated.


If that happens with the correct `.Include()`, you really should raise an issue with EF, trying to reproduce it. If it's not a random mistake in your code, that's a really big deal.


Like your parent said, the same line of code will or won't populate the navigation property depending on whether EF is already tracking the entity that belongs there (generally because some other earlier query loaded it). You get different behavior depending on the state of the system; you can't look at "one line of code" in isolation unless that line of code includes every necessary step to protect itself against context sensitivity.


EF Core 8? Inconsistent behavior is not expected.

Assuming you haven't missed to add an .Include[0], please consider submitting an issue(s) to https://github.com/dotnet/efcore

[0] https://learn.microsoft.com/en-us/ef/core/querying/related-d...


Are you saying that in previous versions inconsistent behaviour is the expected outcome?


I've been using Entity Framework for the last 5 years and have not encountered this issue, as long as I've got all my Includes specified correctly.

There is also the AutoIncludeAttribute that you can specify on entity fields directly to always include those fields for every query.

My main complaints with EF are that the scaffolding and migration commands for the CLI tool are nearly impossible to debug if they error during the run.

But when they run right, they save me a ton of time in managing schema changes. Honestly, I consider that part worth all the rest.

There can also be some difficulty getting queries right when there are cyclical references. Filtering "parent" entities based on "child" ones in a single query can also be difficult, and also can't be composed with Expression callbacks.

But in any difficult case, I can always fall back on ADO.NET with a manual query (there are also ways of injecting manual query bits into EF queries). Which is what we'd be doing without EF, so I don't get the complaints about EF "getting in the way".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: