所以事情是这样的 - 我目前正在使用 EF Core 3.1,假设我有一个实体:
public class Entity
{
public int Id { get; set; }
public int AnotherEntityId { get; set; }
public virtual AnotherEntity AnotherEntity { get; set; }
}
当我访问DbSet<Entity> Entities
正常方式,我包括 AnotherEntity,如:
_context.Entities.Include(e => e.AnotherEntity)
这有效。为什么不呢,对吧?然后我去:
_context.Entities.FromSqlRaw("SELECT * FROM Entities").Include(e => e.AnotherEntity)
这也有效。两者都返回与 AnotherEntity 连接的相同对象集合。然后我使用一个包含相同查询的存储过程SELECT * FROM Entities
名为 spGetEntities:
_context.Entities.FromSqlRaw("spGetEntities")
你猜怎么了?这也有效。显然,它给了我相同的输出,但没有加入 AnotherEntity。但是,如果我尝试像这样添加包含:
_context.Entities.FromSqlRaw("spGetEntities").Include(e => e.AnotherEntity)
我正进入(状态:
使用不可组合 SQL 调用 FromSqlRaw 或 FromSqlInterpolated
并在其上编写查询。考虑打电话AsEnumerable
在 FromSqlRaw 或 FromSqlInterpolated 方法执行之后
客户端的组成。
即使输出_context.Entities.FromSqlRaw("SELECT * FROM Entities")
and _context.Entities.FromSqlRaw("spGetEntities")
是相同的。
我找不到证据证明我可以或不能使用 EF Core 3.1 做到这一点,但如果有人能给我任何关于这种方法可能性的提示,那就太好了。
另外,如果有另一种方法可以使用存储过程获取连接实体,我可能会接受它作为我的问题的解决方案。