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

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

问题描述

按照 https://docs.microsoft.com/zh-cn/ef/core/querying/raw-sql

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

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();

但这意味着我必须创建 DbSet 上下文中的 MyStoredProcResultType ,现在对此进行编码是合法的

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(..)

但是当然这不起作用。

but of course this does not work.

有没有一种方法可以针对 Context 而不是 DbSet调用存储的proc 并将结果放入我创建的类型中?

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天全站免登陆