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

InMemory should not enforce relational behavior when invoking Include on an invalid navigational property #13078

Closed
gojanpaolo opened this issue Aug 22, 2018 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@gojanpaolo
Copy link

I believe that InMemory should not enforce relational behavior when invoking Include on an invalid navigational property.
One of the main reasons is that InMemory doesn't enforce relational behavior when adding entities, so it should also exhibit the same behavior when reading entities.

Given the sample below, ctx.Posts.Include(p => p.Blog) should have the same behavior as ctx.Posts given a Post entity with no Blog.

using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using Xunit;
public class Tests
{
    [Fact]
    public void Add_Post_with_no_Blog_is_ok()
    {
        var options = new DbContextOptionsBuilder<Context>()
            .UseInMemoryDatabase(Guid.NewGuid().ToString())
            .Options;

        using (var ctx = new Context(options))
        {
            ctx.Add(new Post());
            ctx.SaveChanges();
        }

        using(var ctx=  new Context(options))
        {
            var post = ctx.Posts.Single();
            Assert.NotNull(post);
        }
    }

    [Fact]
    public void Read_Post_Include_Blog_when_no_Blog_is_not_ok()
    {
        var options = new DbContextOptionsBuilder<Context>()
            .UseInMemoryDatabase(Guid.NewGuid().ToString())
            .Options;

        using (var ctx = new Context(options))
        {
            ctx.Add(new Post());
            ctx.SaveChanges();
        }

        using (var ctx = new Context(options))
        {
            var post = ctx.Posts.Include(p => p.Blog).Single();
            Assert.NotNull(post);
        }
    }
}
public class Context : DbContext
{
    public Context(DbContextOptions<Context> options) : base(options) { }
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}
public class Post
{
    public int PostId { get; set; }
    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}
public class Blog
{
    public int BlogId { get; set; }
}

I also read #2166 and #9470 which seem to be related but I'm confused whether this was already decided or not.

Further technical details

EF Core version: 2.1.2
Database Provider: Microsoft.EntityFrameworkCore.InMemory
Operating system: Win7
IDE: Visual Studio 2017 15.4

@ajcvickers
Copy link
Contributor

@gojanpaolo We intend to maintain the semantics of FK relationships in the in-memory database, including at some point adding constraint checking. However, we discussed this in triage and we believe that it would be useful to specify an "unconstrained" FK relationship, whether targeting in-memory or relational database. I have opened #13146 to track this.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Aug 28, 2018
@gojanpaolo
Copy link
Author

@ajcvickers Noted. Thanks! :)

@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

2 participants