EF Core 3 DbQuery equivalent functionality

*爱你&永不变心* 提交于 2020-05-15 19:12:09

问题


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;}
}

and then

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

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;}
}

and in ModelCreating

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

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

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?


回答1:


Problem solved! just used this configuration.

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


来源:https://stackoverflow.com/questions/59427708/ef-core-3-dbquery-equivalent-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!