In Entity Framework Core 3.1.3, I have used the value object feature. In the query side, the problem is an extra left join exists in T-SQL. This extra join results in problems in terms of performance. In the following code Student is an Entity type and Address class is a value type.
Entities
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
}
DbContext
public class ApplicationDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Student>().OwnsOne(e => e.Address);
base.OnModelCreating(builder);
}
}
Entity Framework query
var list = _dbContext.Students.ToList();
Generated T-SQL for this EF query:
SELECT [s].[Id], [s].[Name], [t].[Id], [t].[Address_City],
[t].[Address_Street], [t].[Address_ZipCode]
FROM [Students] AS [s]
LEFT JOIN (
SELECT [s0].[Id], [s0].[Address_City],
[s0].[Address_Street], [s0].[Address_ZipCode]
FROM [Students] AS [s0]
WHERE [s0].[Address_ZipCode] IS NOT NULL OR
([s0].[Address_Street] IS NOT NULL OR
[s0].[Address_City] IS NOT NULL)
) AS [t] ON [s].[Id] = [t].[Id]