没有 DbSet 的实体框架核心存储过程 [英] Entity Framework Core Stored Proc without DbSet

查看:19
本文介绍了没有 DbSet 的实体框架核心存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照 https://docs.microsoft.com/en-us/ef/core/querying/raw-sql

但是我专门创建了一个类来匹配从存储过程返回的数据的形状

But I created a class specifically that matches the shape of data returned from the stored proc

List<MyStoredProcResultType> results = context.MyStoredProcResultType
    .FromSql("EXECUTE dbo.MyStoredProc {0}", someParam)
    .ToList();

但这意味着我必须在 Context 中为 MyStoredProcResultType 创建一个 DbSet 并且现在对此进行编码是合法的

But this means I have to create a DbSet for MyStoredProcResultType in the Context and now it is legal to code this

context.MyStoredProcResultType.Where(..)

但这当然行不通.

有没有办法针对 Context 而不是 DbSet 调用存储过程并将结果放入我创建的类型中?

Is there a way to call the stored proc against the Context rather than a DbSet and put the results into a type I create?

推荐答案

目前,在 EF Core 2.0 RTM 版本中,FromSql 只能用于模型中定义为 EntityType 的类型.之所以存在限制,是因为 EF Core 具有关于 EntityType 的元数据,并且知道如何实现它们.因此,对于任何其他类型(如自定义 DTO/结果类型),FromSql 不起作用.

Presently, in EF Core 2.0 RTM version, FromSql can be used only with the types which are defined as EntityType in your model. The restriction is there because, EF Core has metadata about the EntityType and knows how to materialize them. So for any other type (like custom DTO/result type), FromSql does not work.

这是 EF Core GitHub 存储库上的跟踪问题,它正在准确跟踪您的内容正在寻找.

This is tracking issue on EF Core GitHub repository which is tracking exactly what you are looking for.

EF Core 2.1.0 计划添加一些对查询视图的支持.跟踪问题.如果您同意让 EF Core 知道您要在模型构建期间使用的自定义类型,那么这可以启用像您这样的场景.它不会映射为 EntityType/Table,但 EF Core 仍会围绕它计算元数据以支持上述查询.

EF Core 2.1.0 is planning to add some support for querying views. Tracking issue. Which could enable scenario like yours, if you are ok with letting EF Core know about the custom type you want to use during model building. It wouldn't mapped as EntityType/Table but EF Core would still compute metadata around it to support queries like above.

这篇关于没有 DbSet 的实体框架核心存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆