我是新的EF核心,我试图让它与我的ASP工作。NET Core项目。

当我尝试配置DbContext使用config中的连接字符串时,我在startup.cs中得到了上述错误。我正在遵循这个教程。

有问题的代码在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));
        }

如果我把UseSqlServer方法直接放到上下文中,它就会被识别出来:

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

我在网上的所有研究都指向了缺失的参考文献,但我似乎找不到我缺失的是哪一本(见图)。


当前回答

安装下面的NuGet包将解决您的问题

Microsoft.EntityFrameworkCore.SqlServer

安装包Microsoft.EntityFrameworkCore.SqlServer

其他回答

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.

首先添加Install-Package Microsoft.EntityFrameworkCore.SqlServer

接下来使用Microsoft.EntityFrameworkCore添加到你的.cs文件中;

最后将其添加到你的核心Startup.cs中

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));
        }

对于asp.net核心版本2.1,请确保添加以下包来解决这个问题。(至少这修复了使用SQLite的问题)

dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Design

下面是使用带有实体框架核心的SQLite的文档参考。 https://learn.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite

对于仍然有这个问题的人: 使用NuGet安装: Microsoft.EntityFrameworkCore.Proxies

这个问题与使用EFCore的Castle Proxy有关。

我阅读并做了每一个答案的一切指示,我才发现我的问题是在我的DbContext上缺少构造函数

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

我希望这篇文章能帮助那些和我面临同样问题的人。