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

View File

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