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

Selecting a single column from a Nullable child Entity queries the whole object #6199

Closed
IvanJosipovic opened this issue Jul 28, 2016 · 1 comment

Comments

@IvanJosipovic
Copy link

IvanJosipovic commented Jul 28, 2016

Steps to reproduce

http://pastebin.com/mXCLp8du

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace EFBug
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var db = new TestContext())
            {
                db.Parent.Add(new Parent { Child = new Child() {ChildData = "Childtest"} });
                var count = db.SaveChanges();

                string data1 = db.Parent.Select(x => x.Child.ChildData).First();

                //Query is
                //SELECT TOP(1) [x.Child].[ChildData]
                //FROM [Parent] AS[x]
                //INNER JOIN [Child] AS[x.Child] ON[x].[ChildId] = [x.Child].[Id]


                string data2 = db.Parent.Select(x => x.NullableChild.ChildData).First();

                //Query is 
                //SELECT [x].[Id], [x].[ChildId], [x].[NullableChildId], [x.NullableChild].[Id], [x.NullableChild].[ChildData], [x.NullableChild].[ChildData1], [x.NullableChild].[ChildData2], [x.NullableChild].[ChildData3]
                //FROM [Parent] AS[x]
                //LEFT JOIN [Child] AS[x.NullableChild] ON[x].[NullableChildId] = [x.NullableChild].[Id]
                //ORDER BY [x].[NullableChildId]

            }
        }
    }

    public class TestContext : DbContext
    {
        public DbSet<Parent> Parent { get; set; }
        public DbSet<Child> Child { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFBug;Trusted_Connection=True;");
        }
    }

    public class Parent
    {
        public Parent() {}

        [Key]
        public int Id { get; set; }

        [ForeignKey("NullableChild")]
        public int? NullableChildId { get; set; }
        public Child NullableChild { get; set; }

        [ForeignKey("Child")]
        public int ChildId { get; set; }
        public Child Child { get; set; }
    }

    public class Child
    {
        public Child(){}

        [Key]
        public int Id { get; set; }

        public string ChildData { get; set; }
        public string ChildData1 { get; set; }
        public string ChildData2 { get; set; }
        public string ChildData3 { get; set; }
    }
}

The issue

When selecting a single column from a Nullable child Entity, the whole Object is selected rather than a single property. See code above.

string data1 = db.Parent.Select(x => x.Child.ChildData).First();

//Query is
//SELECT TOP(1) [x.Child].[ChildData]
//FROM [Parent] AS[x]
//INNER JOIN [Child] AS[x.Child] ON[x].[ChildId] = [x.Child].[Id]


string data2 = db.Parent.Select(x => x.NullableChild.ChildData).First();

//Query is 
//SELECT [x].[Id], [x].[ChildId], [x].[NullableChildId], [x.NullableChild].[Id], [x.NullableChild].[ChildData], [x.NullableChild].[ChildData1], [x.NullableChild].[ChildData2], [x.NullableChild].[ChildData3]
//FROM [Parent] AS[x]
//LEFT JOIN [Child] AS[x.NullableChild] ON[x].[NullableChildId] = [x.NullableChild].[Id]
//ORDER BY [x].[NullableChildId]

Further technical details

EF Core version: 1.0.0
Operating system: Windows 10
Visual Studio version: (e.g. VS 2013 or n/a) Visual Studio 2015

Other details about my project setup:

@maumar
Copy link
Contributor

maumar commented Jul 28, 2016

dupe of #4588

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

3 participants