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

Entities are not being tracked #34130

Open
ynanech opened this issue Jul 1, 2024 · 1 comment
Open

Entities are not being tracked #34130

ynanech opened this issue Jul 1, 2024 · 1 comment

Comments

@ynanech
Copy link

ynanech commented Jul 1, 2024

I am using EF Core to implement entity updates. After executing method one, there are no changes in the database. However, method two correctly updates the database. The only difference between the two methods is that in method one, userIds is of type IQueryable, while in method two, userIds is of type int[]. Why is it that the entities queried in method one are not being tracked?

        public async Task UpdatePostOne()
        {
            var userIds = _dbContext.Set<User>().Where(m => m.UserId == 123).Select(m => m.UserId);

            var posts= await _dbContext.Set<Post>()
                .Where(m => userIds.Contains(m.UserId) && !m.Deleted)
                .ToArrayAsync();

            foreach (var item in posts)
            {
                item.View+= 100;
            }
            await _dbContext.SaveChangesAsync();
        }


        public async Task UpdatePostTwo()
        {
            int[] userIds = [123];

            var posts= await _dbContext.Set<Post>()
                .Where(m => userIds.Contains(m.UserId) && !m.Deleted)
                .ToArrayAsync();

            foreach (var item in posts)
            {
                item.View+= 100;
            }
            await _dbContext.SaveChangesAsync();
        }

Include provider and version information

EF Core version: 8.0.6
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 8.0
Operating system: Win10
IDE: (e.g. Visual Studio 2022 17.10.3)

@roji
Copy link
Member

roji commented Jul 1, 2024

@ynanech please submit a minimal, runnable code sample that shows the problem - a snippet isn't enough for us to investigate. Your first variant includes the query on the Users DbSet into the main query - that might not be returning the results you're expecting.

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

No branches or pull requests

2 participants