Fix owned relations automatic includes

This commit is contained in:
2023-11-06 14:28:29 +01:00
parent a2d13731e8
commit d7c4da369a
2 changed files with 22 additions and 9 deletions

View File

@@ -32,6 +32,8 @@ namespace BasicSample
[Projectable] [Projectable]
public IEnumerable<Product> FindOrderedProducts(string namePrefix) public IEnumerable<Product> FindOrderedProducts(string namePrefix)
=> Orders.SelectMany(x => x.Items).Select(x => x.Product).Where(x => x.Name.StartsWith(namePrefix)); => Orders.SelectMany(x => x.Items).Select(x => x.Product).Where(x => x.Name.StartsWith(namePrefix));
public ItemImage Image { get; set; }
} }
public class Product public class Product
@@ -65,6 +67,11 @@ namespace BasicSample
public double TotalPrice => Quantity * UnitPrice; public double TotalPrice => Quantity * UnitPrice;
} }
public class ItemImage
{
public string Image { get; set; }
}
public class ApplicationDbContext : DbContext public class ApplicationDbContext : DbContext
{ {
public ApplicationDbContext(DbContextOptions options) : base(options) public ApplicationDbContext(DbContextOptions options) : base(options)
@@ -74,6 +81,13 @@ namespace BasicSample
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.Entity<OrderItem>().HasKey(x => new { x.OrderId, x.ProductId }); modelBuilder.Entity<OrderItem>().HasKey(x => new { x.OrderId, x.ProductId });
modelBuilder.Entity<User>().OwnsOne<ItemImage>(x => x.Image);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
} }
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }
@@ -91,7 +105,7 @@ namespace BasicSample
.AddDbContext<ApplicationDbContext>((provider, options) => { .AddDbContext<ApplicationDbContext>((provider, options) => {
options options
.UseSqlite(dbConnection) .UseSqlite(dbConnection)
// .LogTo(Console.WriteLine) .LogTo(Console.WriteLine)
.EnableSensitiveDataLogging() .EnableSensitiveDataLogging()
.UseProjectables(); .UseProjectables();
}) })
@@ -106,6 +120,7 @@ namespace BasicSample
var user = new User { var user = new User {
FirstName = "Jon", FirstName = "Jon",
LastName = "Doe", LastName = "Doe",
Image = new ItemImage { Image = "foo" },
Orders = new List<Order> { Orders = new List<Order> {
new Order { new Order {
Items = new List<OrderItem> { Items = new List<OrderItem> {

View File

@@ -291,17 +291,15 @@ namespace EntityFrameworkCore.Projectables.Services
} }
var properties = entityType.GetProperties() var properties = entityType.GetProperties()
.Where(x => !x.IsShadowProperty()) .Cast<IReadOnlyPropertyBase>()
.Select(x => x.PropertyInfo as MemberInfo ?? x.FieldInfo!)
.Concat( .Concat(
entityType.GetNavigations() entityType.GetNavigations()
.Where(x => !x.IsShadowProperty()) .Cast<IReadOnlyNavigationBase>()
.Select(x => x.PropertyInfo as MemberInfo ?? x.FieldInfo!) .Concat(entityType.GetSkipNavigations())
.Concat(entityType.GetSkipNavigations() .Where(x => _includedRelations.Contains(x.Name) || x.IsEagerLoaded)
.Where(x => !x.IsShadowProperty())
.Select(x => x.PropertyInfo as MemberInfo ?? x.FieldInfo!))
.Where(x => _includedRelations.Contains(x.Name))
) )
.Where(x => !x.IsShadowProperty())
.Select(x => x.PropertyInfo as MemberInfo ?? x.FieldInfo!)
// Remove projectable properties from the ef properties. // Remove projectable properties from the ef properties.
.Where(x => projectableProperties.All(y => x.Name != y.Name)); .Where(x => projectableProperties.All(y => x.Name != y.Name));