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

EFC8: Lazy Loading regression on newly created objects #32314

Closed
Suchiman opened this issue Nov 15, 2023 · 0 comments · Fixed by #32343
Closed

EFC8: Lazy Loading regression on newly created objects #32314

Suchiman opened this issue Nov 15, 2023 · 0 comments · Fixed by #32343
Assignees
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@Suchiman
Copy link
Contributor

Suchiman commented Nov 15, 2023

When creating a new object that uses ILazyLoader, the LazyLoader will be populated when the object is added to a DbSet.
If you now access a navigation property on the newly created object (e.g. check it for null during initialization and set it otherwise) before having called SaveChanges, EFC8 will now throw the following error:

System.InvalidOperationException: The navigation 'Post.Blog' cannot be loaded because one or more of the key or foreign key properties are shadow properties and the entity is not being tracked. Relationships using shadow values can only be loaded for tracked entities.
   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.GetLoadValues(INavigation navigation, InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.Load(INavigation navigation, InternalEntityEntry entry, LoadOptions options)
   at Microsoft.EntityFrameworkCore.ChangeTracking.ReferenceEntry.Load(LoadOptions options)
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.LazyLoader.Load(Object entity, String navigationName)
   at Microsoft.EntityFrameworkCore.Infrastructure.LazyLoaderExtensions.Load[TRelated](ILazyLoader loader, Object entity, TRelated& navigationField, String navigationName)
   at Post.get_Blog()

In EFC7 there was no such error being generated.

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

public class Program
{
    public static void Main()
    {
        var ctx = new BloggingContext();
        ctx.Database.EnsureDeleted();
        ctx.Database.EnsureCreated();

        var post = new Post();
        ctx.Posts.Add(post);
        //ctx.SaveChanges();
        _ = post.Blog;
    }
}

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseSqlite($"Data Source=db.sqlite");
    }
}

public class Blog
{
    public int BlogId { get; set; }

    private ICollection<Post>? _posts;

    public ICollection<Post> Posts
    {
        get => LazyLoader?.Load(this, ref _posts);
        set => _posts = value;
    }

    public ILazyLoader? LazyLoader { get; set; }
}

public class Post
{
    public int PostId { get; set; }

    private Blog? _blog;
    public Blog? Blog
    {
        get => LazyLoader?.Load(this, ref _blog);
        set => _blog = value;
    }

    public ILazyLoader? LazyLoader { get; set; }
}

I currently see two possible workarounds: Catch and discard the exception or delay associating the newly created object with the DbContext for as long as possible.

Include provider and version information

EF Core version: 8.0
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 8.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.8

@ajcvickers ajcvickers self-assigned this Nov 16, 2023
@ajcvickers ajcvickers added this to the 8.0.x milestone Nov 17, 2023
ajcvickers added a commit that referenced this issue Nov 18, 2023
…known FK values

Fixes #32314

We added this check primarily for lazy-loading of untracked entities. However, it causes a regression when accessing navigation properties on newly Added entities, where a no-op is reasonable.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 18, 2023
@ajcvickers ajcvickers reopened this Nov 20, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.1 Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants