Efcore: EntityFrameworkCore FromSQL Not working if we create a Class Library Project

Created on 16 Nov 2018  Â·  10Comments  Â·  Source: dotnet/efcore

I am getting the error as given below, if I write FromSql in a class Library Project.

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

Steps to recreate the error

Step:1

Created a Class Library Project

Step: 2

```C#
public class DbContextFactory : IDesignTimeDbContextFactory
{
public MyContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder();

        return new MyContext(optionsBuilder.Options);
    }
}
Step:3
=====
```C#
 public class MyContext : DbContext
    {
        public MyContext()
        {
        }

        public MyContext(DbContextOptions<MyContext> options)
            : base(options)
        {
        }

        //Proc Results
        public DbSet<SearchResult> SearchResults { get; set; }
    }

Step:4

Created a Model for SearchResult.
```C#
public class SearchResult
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
}

Step: 5
====
Created a Repository
```C#
    public class SearchRepository  : BaseRepository
    {
        public SearchRepository(MyContext context) : base(context)
        {

        }

        public async Task<IEnumerable<SearchResult>> GetSearchByIdAsync(int id)
        {

            SqlParameter param = new SqlParameter() {
                                ParameterName = "@Id",
                                SqlDbType =  System.Data.SqlDbType.Int,
                                Direction = System.Data.ParameterDirection.Input,
                                Value = id
            };

            var result = await _ocmsContext.SearchResults.FromSql("ProcName @Id", param).ToListAsync();
            return result;
        }
    }

Step: 6

In the Asp.Net webapi core project, Added the Reference to the Class Library Project.
Code written In the startup class

```C#
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        var connection = Configuration["ConnectionStrings:MyConnectionString"];
        services.AddDbContext<OCMSContext>(options => options.UseSqlServer(connection));

        services.AddScoped<ISearchRepository, SearchRepository>();
    }


After doing all the above code, I am getting the compilation error at the Step 5, for FromSql (Error message given below)

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


Note, If I move the SearchRepository to the Web.Api Core project, the FromSql is not showing any error, it is working fine.

Please advice, what am I missing.   

Further technical details

EF Core version: (found in project.csproj or packages.config)
Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Operating system:
IDE: (e.g. Visual Studio 2017 15.4)

closed-question customer-reported

Most helpful comment

Sorry, it is my bad.
I am able to install the same using the NuGet packages and the error is gone.

Thanks for prompting the correct direction.

All 10 comments

Please share csproj file of your class library project.

Hi Smit Patel.

As requested I am attaching herewith the sample Test solution with the
class library project.

Please help me.

Thanks

Srinivasan Hariharan

On Fri, Nov 16, 2018 at 10:58 AM Smit Patel notifications@github.com
wrote:

Please share csproj file of your class library project.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/EntityFrameworkCore/issues/13972#issuecomment-439438456,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AaPNkveizws4_L3oq19t1BqWH870CAXgks5uvuC2gaJpZM4Ymbec
.

--
Regards,


Srinivasan Hariharan
New Jersey 08536

@hsvasan - I am unable to find your sample.

I have attached the zip file to my email. Did u recived the attachment if
not.. let me know your contact, I will give your call right now. I am in
NY.

Thanks
Srinivasan Hariharan

On Fri, Nov 16, 2018 at 3:16 PM Smit Patel notifications@github.com wrote:

@hsvasan https://github.com/hsvasan - I am unable to find your sample.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/EntityFrameworkCore/issues/13972#issuecomment-439514346,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AaPNkoXPOPmH4lpp_L-vXZCtlGMUlYQJks5uvx0vgaJpZM4Ymbec
.

--
Regards,


Srinivasan Hariharan
New Jersey 08536

@hsvasan - If you reply to a comment via mail then attachments are ignored. You would need to open this in your web browser and then attach the project with your comment.

TestSolution.zip
Sample file attached

Thanks for the reply.

File attached to the comment

Srinivasan Hariharan

On Fri, Nov 16, 2018 at 3:45 PM Smit Patel notifications@github.com wrote:

@hsvasan https://github.com/hsvasan - If you reply to a comment via
mail then attachments are ignored. You would need to open this in your web
browser and then attach the project with your comment.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/aspnet/EntityFrameworkCore/issues/13972#issuecomment-439521693,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AaPNkj_PzkCdPobmx8k_vlTEYHqEpEwpks5uvyPcgaJpZM4Ymbec
.

--
Regards,


Srinivasan Hariharan
New Jersey 08536

You are referencing Microsoft.EntityFrameworkCore.dll directly in your TestData project. FromSql is relational database specific method. In order to get it you would need to reference `Microsoft.EntityFrameworkCore.Relational in your TestData project.

I am not getting that reference. Can you please add that in the sample and attach it here. Understand from other forums, it is related to core 2.0 and from core 2.1 it is inside the core. If it is working for you can you change the reference and add the sample file to the comment please.

Sorry, it is my bad.
I am able to install the same using the NuGet packages and the error is gone.

Thanks for prompting the correct direction.

Was this page helpful?
0 / 5 - 0 ratings