Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error CS1061: 'DbSet<T>' does not contain a definition for 'FromSql' and no extension method 'FromSql' accepting a first argument of type 'DbSet<T>'

I am trying to call view or store procedure using asp.net core 2.1 on mac os webapi.

using System;
using System.Linq;
using Auth.Database;
using Microsoft.EntityFrameworkCore;

public virtual IQueryable<T> ExecuteStoreProcView(string viewProcName)
{
    IQueryable<T> queryResult = _entities.Set<T>().FromSql(viewProcName).AsQueryable();
    return queryResult;
}

Getting the below error

Error CS1061: 'DbSet' does not contain a definition for 'FromSql' and no extension method 'FromSql' accepting a first argument of type 'DbSet' could be found (are you missing a using directive or an assembly reference?) (CS1061)

I am developing webapi using entity framework on mac os.

Research some of the queries in below link :- Raw SQL Query without DbSet - Entity Framework Core

Raw SQL Query without DbSet - Entity Framework Core

https://forums.asp.net/t/1886501.aspx?System+Data+Entity+DbSet+Entities+User+does+not+contain+a+definition+for+FirstOrDefault+

https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationalqueryableextensions.fromsql?view=efcore-2.1

But not able to find the error solution. Can anyone please let me know what I, am missing.

like image 448
San Jaisy Avatar asked Jun 25 '18 06:06

San Jaisy


People also ask

What is the use of FromSql method in Entity Framework core method?

Entity Framework Core provides the DbSet. FromSql() method to execute raw SQL queries for the underlying database and get the results as entity objects.

What is FromSqlRaw?

FromSqlRaw allows you to use named parameters in the SQL query string, which is useful when a stored procedure has optional parameters: C# Copy.


2 Answers

install Microsoft.EntityFrameworkCore.Relational nuget package and then add the using statement to the class

using Microsoft.EntityFrameworkCore;

like image 78
Sahil Avatar answered Sep 20 '22 18:09

Sahil


This might have changed since Umer answered. It is now in the Microsoft.EntityFrameworkCore namespace. But you will need to reference the package.

So...

dotnet add package Microsoft.EntityFrameworkCore.Relational

And add this line...

using Microsoft.EntityFrameworkCore;
like image 45
coderpatros Avatar answered Sep 21 '22 18:09

coderpatros