Skip to content

Conversation

@hahn-kev
Copy link
Contributor

@hahn-kev hahn-kev commented May 2, 2025

closes #129

The issue is that you can't query entities and have them be tracked, so this fails:

var entity = db.Entities.First();
db.Remove(entity);//throws because entity isn't tracked

This is because db.Entities.First() is rewritten into db.Entities.Select(e => new Entity { ...e }).First() and this breaks entity tracking.

So I introduced some fixes so that if tracking was going to happen then it wouldn't be rewritten. This is determined via the default tracking behavior, configured like:

optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll); //default value shown

or based on if AsTracking or AsNoTracking` is part of the query, eg:

var entity = db.Entities.AsTracking().First();//query is not rewritten
var entity = db.Entities.AsNoTracking().First();//query is rewritten

default applies first and can be overridden by using AsTracking or AsNoTracking explicitly. So if you want root rewriting by default, then I suggest you set the default tracking behavior to NoTracking and everything will keep working as it does in the current release. If however you use tracking by default, then you can call AsNoTracking or ExpandProjectables to rewrite the root query.

@koenbeuk koenbeuk merged commit 0231edf into koenbeuk:master May 4, 2025
4 checks passed
@koenbeuk
Copy link
Owner

koenbeuk commented May 4, 2025

Thanks!

@hahn-kev
Copy link
Contributor Author

hahn-kev commented May 5, 2025

Awesome, when do you think an updated nuget package will be available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EF Core 9 Change Tracking stops working when [Projectable(UseMemberBody=)] is used in an Entity.

2 participants