[英]execute stored procedure in entity Framework Core without expecting map to dbset
I am working on .NET CORE, Entity Framework core.我正在研究 .NET CORE,Entity Framework 核心。 I have stored procedure that I need to execute from .NET class.
我有需要从 .NET 类执行的存储过程。 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 这是最终的例外。
I wounder if I can use my dataView instead of context.dataModel class, current implementation (Context.Claims.FromSql)如果我可以使用我的 dataView 而不是 context.dataModel 类,当前实现 (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);
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()
You can utilize the Query Types introduced in EF Core 2.1.您可以使用 EF Core 2.1 中引入的 查询类型。
First you need to register you class as query type:首先,您需要将您的类注册为查询类型:
modelBuilder.Query<ClaimDataView>();
Then you can use Context.Query<ClaimDataView>()
in place of your current Context.Claims
:然后您可以使用
Context.Query<ClaimDataView>()
代替您当前的Context.Claims
:
var query = Context.Query<ClaimDataView>().FromSql(...);
Update (EF Core 3.x+) :更新(EF Core 3.x+) :
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从EF Core 3.0开始,query types 和entity types合并,改名为Keyless Entity Types ,所以对应的代码是
modelBuilder.Entity<ClaimDataView>().HasNoKey().ToView(null);
and和
var query = Context.Set<ClaimDataView>().FromSql(...);
If you are not on version 2.1, you will need to add:如果您使用的不是 2.1 版,则需要添加:
public DbSet<ClaimDataView> ClaimDataView { get; set; }
to your moduleContext.到你的模块上下文。 And add NotMapped to your class:
并将 NotMapped 添加到您的班级:
[NotMapped]
public class ClaimDataView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.