在 EF Core 中,在具有 OwnedEntity 的实体上使用 FromSqlInterpolated() 方法查询不起作用

In EF Core, querying with FromSqlInterpolated() method on an entity that has an OwnedEntity doesn't work

我的上下文中有一些复杂的实体。

DbSet<EntityA> AEntities { get; set; }
DbSet<EntityB> BEntities { get; set; }

其中 EntityA 继承了 EntityB(它是它的超集)。有一个列 Discriminator 是由 Entity Framework 创建的,用于区分 2。到这里,没问题。

EntityA 也有一个拥有的类型:

modelBuilder.Entity<EntityA>().OwnsOne(p => p.OwnedThing,
                ot =>
                {
                    // ot......;
                    ot.ToTable("SectionTrailInfo");
                });

只要我只用EF查询数据库还是可以的

但是出于性能原因,我需要使用 SQL 存储过程查询 BEntities。

// ...
return await BEntities.FromSqlInterpolated($"EXEC GetManyRecords @Id = {id}").ToListAsync();

这在实体 A 中引入拥有实体之前也有效。现在我收到 'FromSqlRaw' or 'FromSqlInterpolated' was called with non-composable SQL and with a query composing over it. 错误。我一尝试 .ToQueryString() 方法就抛出错误。

我已经尝试将存储过程修改为左连接自有类型,但它没有改变任何事情。事实上,查询永远不会形成,也永远不会发送到数据库服务器。所以我必须以某种方式告诉 EF Core 如何处理拥有的类型(它甚至不是这个实体的一部分,而是一个相关的类型)。

我唯一的其他选择是完全重新设计我的架构以删除拥有的类型,但我希望我可以避免这种情况。

提前致谢。

正如@IvanStoev 所指出的那样,要走的路是在 SQL.

中使用 Table-Valued-Function 而不是存储过程