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

FromSql() and owned type with table splitting #18616

Open
Tracked by #21888
grietine opened this issue Oct 28, 2019 · 2 comments
Open
Tracked by #21888

FromSql() and owned type with table splitting #18616

grietine opened this issue Oct 28, 2019 · 2 comments

Comments

@grietine
Copy link

Entity, ValueObject and OnModelCreating() looks like this:

public class User : IEntity
{
   public Guid Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public Address Address { get; set; }
}

public class Address: IValueObject
{
   public string Street { get; set; }
   public string City { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<User>().ToTable("Users");
    modelBuilder.Entity<User>().OwnsOne(e => e.Address);
}

I am saving Address ValueObject on the same Users table with .OwnsOne().

Issue:

A query on Users table using .FromSql() causes unnecessary LEFT JOIN.

Using .FromSql() on a different entity (e.g. Transactions) which doesn't have a ValueObject in it works OK and doesn't include LEFT JOIN in the query.

Query example:

var customQuery = "SELECT * FROM Users WHERE CONTAINS(*, '\"John\"')";
var users = context.Users.FromSql(customQuery);

Expected query:

SELECT * FROM Users WHERE CONTAINS(*, '\"John\"');

Actual query:

SELECT [p].[Id],
       [p].[FirstName],
       [p].[LastName],
       [p.Address].[Id],
       [p.Address].[Street],
       [p.Address].[City]
FROM (SELECT * FROM Users WHERE CONTAINS(*, '\"John\"')) AS [p]
       LEFT JOIN [Users] AS [p.Address]
         ON [p].[Id] = [p.Address].[Id]

A bug or am I doing something incorrectly?

Further technical details

EF Core version: 2.2
Database provider: e.g. Microsoft.EntityFrameworkCore.SqlServer 2.2.6
Target framework: NET Core 2.2
Operating system: Windows 10 Enterprise
IDE: Visual Studio 2017

@ajcvickers ajcvickers added this to the Backlog milestone Oct 28, 2019
@AndriySvyryd AndriySvyryd changed the title Using FromSql() on DbSet<Entity> with ValueObject on same table should be without LEFT JOIN FromSql() and owned type with table splitting Oct 28, 2019
@smitpatel
Copy link
Contributor

Related #14525

@AndriySvyryd
Copy link
Member

Depends on #21627

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

4 participants