EF Core 3 DbQuery 等效功能 [英] EF Core 3 DbQuery equivalent functionality

查看:33
本文介绍了EF Core 3 DbQuery 等效功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ef core 2.2 中,我使用 DbQuery 将原始 sql 结果映射到对象,如下所示:

In ef core 2.2 I have used DbQuery to map raw sql results to object as following:

public partial class AppDbContext{
    public DbQuery<SimpleQueryModel> SimpleQM {get;set;}
}

然后

var result=_dbContext.SimpleQM.FromSql(sqlString,params);

这不会创建任何额外的表并且工作正常.在 ef core 3.1 DbQuery 已过时,并告诉我改用无键 DbSet.我已将其配置如下:

this wouldn't create any extra table and working just fine. In ef core 3.1 DbQuery is obsolete and telling me to use keyless DbSet instead. I have configured it as following:

public partial class AppDbContext{
    public DbSet<SimpleQueryModel> SimpleQM {get;set;}
}

并在模型创建中

builder.Entity<SimpleQueryModel>().HasNoKey();

但这将在新数据库迁移中创建一个新表,如果我告诉 ef 忽略此实体如下

but this will create a new table in new DB migration and if I tell ef to Ignore this entity as following

builder.Entity<SimpleQueryModel>().HasNoKey().Ignore();

我不能使用 _dbContext.SimpleQM.FromSqlRaw(); 这将引发异常并告诉该模型未包含在上下文中.如何在 ef core 3.1 中实现相同的功能?

I can't use _dbContext.SimpleQM.FromSqlRaw(); this will throw an exception and telling that the model is not included in the Context. how can I achieve same functionality in ef core 3.1?

推荐答案

问题解决了!刚用这个配置.

Problem solved! just used this configuration.

builder.Entity<SimpleQueryModel>().HasNoKey().ToView("view_name_that_doesnt_exist");

这篇关于EF Core 3 DbQuery 等效功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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