You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[TestMethod]publicvoidProjectionSelectBug(){intuserPk=1;varuser=Context.UserInfos.Include(p =>p.Privileges).Select(p =>newUserInfo{UserPk=p.UserPk,UserId=p.UserId,FirstName=p.FirstName,LastName=p.LastName,Department=p.Department,Privileges=p.Privileges//.Where(p2 => p2.AccessKey.StartsWith("FTDB_")).ToList()}).FirstOrDefault(u =>u.UserPk==userPk);stringupdatedLastName="Updated Name";user.LastName=updatedLastName;intres=Context.SaveChanges();varnewContext=newFtdbContext();varuser2=newContext.UserInfos.FirstOrDefault(u =>u.UserPk==userPk);// fails - value not updatedAssert.AreEqual(updatedLastName,user2.LastName);};
I'm using the projection to filter the list of privileges. The query returns the correct entity along with the correct child relationship and filtered privileges.
However, the result is not tracked and when I change the entity values then SaveChanges() the changes are not written.
The Assert fails with user2.LastName remaining at the original, unchanged value.
It fails with the .Select() projection. If I remove the projection then it works.
Expected Behavior
I would expect a projection of an Entity Type to be tracked as long as the type is an entity type.
Closing as I realized this is by design (and mostly makes sense 😄).
Wish there was a way to transform an entity though so you can updated or even filter a value more easily (like the example above) - there are other ways, but this certainly would be easiest).
I have an application that does the following:
I'm using the projection to filter the list of privileges. The query returns the correct entity along with the correct child relationship and filtered privileges.
However, the result is not tracked and when I change the entity values then
SaveChanges()
the changes are not written.The Assert fails with user2.LastName remaining at the original, unchanged value.
It fails with the
.Select()
projection. If I remove the projection then it works.Expected Behavior
I would expect a projection of an Entity Type to be tracked as long as the type is an entity type.
According to the docs at https://docs.microsoft.com/en-us/ef/core/querying/tracking#tracking-and-custom-projections, it looks to me that the above should work, but it's unclear on whether the original entity going into the projection has to stay intact. If so that seems silly - as long as an entity type is created it should be tracked.
Config
EF Core version: 3.0
Database provider: . Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3
The text was updated successfully, but these errors were encountered: