在实体Framework Core中执行存储过程而不期望映射到dbset
我正在研究.NET CORE,Entity Framework核心。 我有需要从.NET类执行的存储过程。 我的存储过程需要多个’Context’,我不知道如何处理这个,虽然我有dataView,这是最后的例外。
我是否可以使用我的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);
新的更新
moduleContext类
protected override void OnModelCreating(ModelBuilder modelBuilder) { //other models.... modelBuilder.Query(); }
从中执行的存储过程
var query = Context.Query().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中引入的查询类型 。
首先,您需要将您的类注册为查询类型:
modelBuilder.Query();
然后,您可以使用Context.Query
代替您当前的Context.Claims
:
var query = Context.Query().FromSql(…);
如果您不是2.1版,则需要添加:
public DbSet ClaimDataView { get; set; }
到您的moduleContext。 并将NotMapped添加到您的类:
[NotMapped] public class ClaimDataView