Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 3 DbQuery equivalent functionality

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?

like image 690
Reza Ebadi Avatar asked Dec 20 '19 15:12

Reza Ebadi


People also ask

What is DbQuery?

A DbQuery is a property on the DbContext that acts in a similar way to a DbSet , providing a root for LINQ queries. However, it doesn't enable operations that write to the database e.g. Add . The DbQuery maps to a table or view.

Can Core 3.1 Use EF Core 5?

Technically, EF Core 5 can run on . NET Core 3.1, but aligning versions is always a good idea.

Is EF core faster than EF6?

Note that for a single row, EF Core is slower than EF6 unless using context pooling; this could be the cost of setting up all the scoped services for each instance (which isn't done when context pooling is on). For multiple rows, EF Core batches and EF6 doesn't, so EF Core quickly becomes much faster.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.


1 Answers

Problem solved! just used this configuration.

builder.Entity<SimpleQueryModel>().HasNoKey().ToView("view_name_that_doesnt_exist");
like image 114
Reza Ebadi Avatar answered Oct 17 '22 07:10

Reza Ebadi