Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Select() Projection causes Entity to not be tracked #18825

Closed
RickStrahl opened this issue Nov 9, 2019 · 2 comments
Closed

.Select() Projection causes Entity to not be tracked #18825

RickStrahl opened this issue Nov 9, 2019 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@RickStrahl
Copy link

RickStrahl commented Nov 9, 2019

I have an application that does the following:

 [TestMethod]
public void ProjectionSelectBug()
{
    int userPk = 1;

    var user = Context.UserInfos
        .Include(p => p.Privileges)
        .Select(p => new UserInfo
        {
            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);

    string updatedLastName = "Updated Name";
    user.LastName = updatedLastName;

    int res = Context.SaveChanges();

    var newContext = new FtdbContext();
    var user2 = newContext.UserInfos        
        .FirstOrDefault(u => u.UserPk == userPk);

    // fails - value not updated
    Assert.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.

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

@smitpatel
Copy link
Contributor

Duplicate of #12668

@smitpatel smitpatel marked this as a duplicate of #12668 Nov 9, 2019
@RickStrahl
Copy link
Author

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).

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported and removed type-bug labels Nov 11, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants