I am new to EF core and I'm trying to get it to work with my ASP.NET Core project.
I get the above error in my startup.cs
when trying configure the DbContext
to use a connection string from config. I am following this tutorial.
The problematic code in startup.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using tracV2.models;
using tracV2.data;
namespace tracV2
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
string conn = Configuration.GetConnectionString("optimumDB");
services.AddDbContext<tracContext>(options => options.usesqlserver(conn));
}
The UseSqlServer
method is recognized if I put it directly into the context:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace tracV2.data
{
public class tracContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("myrealconnectionstring");
}
All my research online points to missing references, but I can't seem to find out which one I am missing (see image).
This question is related to
asp.net-core
entity-framework-core
I had this issue, it seems that I hadn't added the required NuGet packages, although I thought I had done so, make sure to check them, one by one.
The Package is missing. Open Package Manager Console and execute the code below:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
As mentioned by top scoring answer by Win you may need to install Microsoft.EntityFrameworkCore.SqlServer NuGet Package, but please note that this question is using asp.net core mvc. In the latest ASP.NET Core 2.1, MS have included what is called a metapackage called Microsoft.AspNetCore.App
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.2
You can see the reference to it if you right-click the ASP.NET Core MVC project in the solution explorer and select Edit Project File
You should see this metapackage if ASP.NET core webapps the using statement
<PackageReference Include="Microsoft.AspNetCore.App" />
Microsoft.EntityFrameworkCore.SqlServer is included in this metapackage. So in your Startup.cs you may only need to add:
using Microsoft.EntityFrameworkCore;
For me this issue happened with Visual Studio Code and I was able to fix with 2 steps:
using Microsoft.EntityFrameworkCore;
dotnet build
in terminal.Project is works in DotNET Core 3.1+ or higher(future)
Add this package:
Microsoft.EntityFrameworkCore.Tools
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.8">
Install-Package:
**Microsoft.EntityFrameworkCore.SqlServer**
then add the top of your class:
**Microsoft.EntityFrameworkCore;**
that's worked for me
Follow the steps below.
Install Entity Framework Core Design and SQL Server database provider for Entity Framework Core:
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Import Entity Framework Core:
using Microsoft.EntityFrameworkCore;
And configure your DbContext:
var connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(connectionString)
);
adding
using Microsoft.EntityFrameworkCore;
manually solved the problem for me
Edit...
for dotnet core 3.1 add
Microsoft.EntityFrameworkCore.SqlServer
I believe this can be solved by adding a project reference to Microsoft.EntityFrameworkCore.SqlServer.Design
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
Microsoft.EntityFrameworkCore.SqlServer wasn't directly installed in my project, but the .Design package will install it anyway as a prerequisite.
Wow so many answers yet none mentioned this Microsoft.EntityFrameworkCore.InMemory package!
Add the reference to this package:
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.2" />
and you should be good to go.
I was using Visual Studio Code.
1) Try to install the package 'Microsoft.EntityFrameworkCore.SqlServer' by specifying the version number.
VS Code:
'dotnet add package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'
Visual Studio:-
'Install-Package Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'
Refer the link 'Package 'Microsoft.EntityFrameworkCore.SqlServer' is incompatible with 'all' frameworks in the project' for doing it.
2) Then add the namespace 'using Microsoft.EntityFrameworkCore;' manually in the Startup.cs file.
Refer the below link https://github.com/aspnet/EntityFramework/issues/7891.
3) If you get any dependency issue for 'Microsoft.EntityFrameworkCore.SqlServer.Design', like "Package 'Microsoft.EntityFrameworkCore.Design' is incompatible with 'all' frameworks in project" ,we need to run the below command,
VS Code:-
dotnet add package Microsoft.EntityFrameworkCore.Design -v 1.1
Visual Studio
Install-Package Microsoft.EntityFrameworkCore.Design -v 1.1
I had this trouble when I moved to Microsoft.EntityFrameworkCore.SqlServer v3.0.0 and Microsoft.EntityFrameworkCore.Tools v3.0.0
When I changed back to v2.2.6 on both libraries, the error went away. This is more of a workaround than a solution but it'll get you up and running till the issue is fixed.
Copying the following code into the TodoApi.csproj from https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/first-web-api/sample/TodoApi solved similar issue for me.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
Microsoft.AspNetCore.All may be excessive but it includes EntityFrameworkCore
In Visual Studio, check the NuGet Package Manager => Manage Packages for Solution, check all this packages, whether got installed in your solution or not, as below:
I solved the same issues after check all the above packages have been installed.
Install package, EntityFrameworkCore.SqlServer:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.3
Nuget: https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/
Install the following packages from Nuget :-
For anyone still having this problem: Use NuGet to install: Microsoft.EntityFrameworkCore.Proxies
This problem is related to the use of Castle Proxy with EFCore.
I had to use the line
services.AddEntityFrameworkSqlite().AddDbContext<MovieContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));
in the ConfigureServices method in the Startup.cs
I installed these three of them in the package manager console and it worked.
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 3.1.4 Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.1.8 Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.8
First we install the Microsoft.EntityFrameworkCore.SqlServer NuGet Package:
PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer
Then, after importing the namespace with
using Microsoft.EntityFrameworkCore;
we add the database context:
services.AddDbContext<AspDbContext>(options =>
options.UseSqlServer(config.GetConnectionString("optimumDB")));
Currently working with Entity Framework Core 3.1.3. None of the above solutions fixed my issue.
However, installing the package Microsoft.EntityFrameworkCore.Proxies on my project fixed the issue. Now I can access the UseLazyLoadingProxies() method call when setting my DBContext options.
Hope this helps someone. See the following article:
I had same issue but problem went off after going back and fixing DbContext
incorrect syntax issue such as it should have been ExampleDbContextClass: DbContext
whereas I had missed DbContext
part in Context Class where you define your DbSet
. Also, I verified following dependencies are needed in order to achieve connection to SqlServer.
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
Also verify that Version you install is less than your project's current version to be on safe side. Such as if your project is using 3.1 do not try to use newer one which would be for example 3.1.9. I had issue with that too.
I also had the same problem. I added the following. It works for me
Microsoft.EntityFrameworkCore.SqlServer
first add Install-Package Microsoft.EntityFrameworkCore.SqlServer
next add in your .cs file using Microsoft.EntityFrameworkCore;
finally add this in your core Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));
}
I got around this by simply:
Add SqlServerDbContextOptionsExtensions
to the class in question
Resolve SqlServerDbContextOptionsExtensions
This fixes the issue, must be missing some reference by default.
your solution works great.
When I saw this video till 17 Minute: https://www.youtube.com/watch?v=fom80TujpYQ I was facing a problem here:
services.AddDbContext<PaymentDetailContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));
UseSqlServer not recognizes so I did this Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.5
& using Microsoft.EntityFrameworkCore;
Then my problem is solved. About me: basically I am a purely PHP programmer since beginning and today only I started .net coding, thanks for good community in .net
Project -> ManageNugetPackages -> Browse -> Search "Microsoft.EntityFrameworkCore.SqlServer" and install or update.
Install below NuGet Package will solve your issue
Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.SqlServer
For asp.net core version 2.1 make sure to add the following package to fix the problem. (At least this fix the issue using SQLite)
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design
Here is the reference of the documentation using SQLite with entity framework core. https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite
Source: Stackoverflow.com