在实体框架核心中执行存储过程而不期望映射到 dbset [英] execute stored procedure in entity Framework Core without expecting map to dbset

查看:28
本文介绍了在实体框架核心中执行存储过程而不期望映射到 dbset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 .NET CORE,Entity Framework 核心.我有需要从 .NET 类执行的存储过程.我的存储过程需要上下文"的数量,但我不知道如何处理这个问题,尽管我有 dataView 这是最后一个例外.

I am working on .NET CORE, Entity Framework core. I have stored procedure that I need to execute from .NET class. My stored procedure takes number of 'Context' and I don't know how to deal this, although I have dataView which is final exception.

如果我可以使用我的 dataView 而不是 context.dataModel 类,当前实现 (Context.Claims.FromSql)

I wounder if I can use my dataView instead of context.dataModel class, current implementation (Context.Claims.FromSql)

public class ClaimDataView
{
    public Guid ClaimId { get; set; }
    public int IdNum { get; set; }
    public string Value { get; set; }
    public DateTime ExpirationDate { get; set; }
    public ClaimAttributionActions Action { get; set; }
    public bool ActiveStateForRole { get; set; }

}

存储过程调用

public Guid UserId { get; set; }
public Guid ClientId { get; set; }
public Guid ConsultationId { get; set; }

  var userParam = new SqlParameter("@UserVal", UserId);
  var clientParam = new SqlParameter("@ClientVal", ConsultationId);
  var consultationParam = new SqlParameter("@ConsultationVal", ConsultationId);

 //**************need help in following line
  var query = Context.Claims.FromSql("EXECUTE dbo.ListUserClaims @userId=@UserVal, @clientId=@ClientVal, @consultationId=@ConsultationVal"
            , userParam, clientParam, consultationParam);

新更新

moduleContext 类

  protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
       //other models....
       modelBuilder.Query<ClaimDataView>();
    }

存储过程执行自

 var query = Context.Query<UserDataView>().FromSql("EXECUTE dbo.ListUserClaims @userId=@UserVal, @clientId=@ClientVal, @consultationId=@ConsultationVal"
            , userParam, clientParam, consultationParam);

错误

System.InvalidOperationException: Cannot create a DbSet for 'UserDataView' because this type is not included in the model for the context.
 at Microsoft.EntityFrameworkCore.Internal.InternalDbQuery`1.get_EntityType()

推荐答案

您可以利用 查询类型在 EF Core 2.1 中引入.

You can utilize the Query Types introduced in EF Core 2.1.

首先,您需要将类注册为查询类型:

First you need to register you class as query type:

modelBuilder.Query<ClaimDataView>();

然后你可以使用 Context.Query() 代替你当前的 Context.Claims:

Then you can use Context.Query<ClaimDataView>() in place of your current Context.Claims:

var query = Context.Query<ClaimDataView>().FromSql(...);

<小时>

更新(EF Core 3.x+):

从 EF Core 3.0 开始,查询类型一直是 与实体类型合并 并重命名为 Keyless Entity Types,所以对应的代码是

Starting with EF Core 3.0, query types have been consolidated with entity types and renamed to Keyless Entity Types, so the corresponding code is

modelBuilder.Entity<ClaimDataView>().HasNoKey().ToView(null);

var query = Context.Set<ClaimDataView>().FromSql(...);

这篇关于在实体框架核心中执行存储过程而不期望映射到 dbset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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