我是新的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");
        }

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


当前回答

项目工作在DotNET核心3.1+或更高(未来)

添加这个包:

NuGet: Microsoft.EntityFrameworkCore.Tools 项目配置(.csproj): <PackageReference Include="Microsoft.EntityFrameworkCore. "工具3.1.8中“Version = >

其他回答

我用的是Visual Studio Code。

1)尝试安装“Microsoft.EntityFrameworkCore”包。SqlServer',指定版本号。

VS代码:

添加Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1

Visual Studio: -

安装包Microsoft.EntityFrameworkCore.SqlServer -v 1.1.1'

请参考链接“Package”Microsoft.EntityFrameworkCore。SqlServer'与项目中的'所有'框架'不兼容。

2)然后在Startup.cs文件中手动添加命名空间“using Microsoft.EntityFrameworkCore;”

参考下面的链接 https://github.com/aspnet/EntityFramework/issues/7891。

3)如果你对'Microsoft.EntityFrameworkCore.SqlServer. net有任何依赖问题。“设计”,比如“包”Microsoft.EntityFrameworkCore。“设计”与“项目中的“所有”框架不兼容”,我们需要运行以下命令,

VS代码:

. net添加包Microsoft.EntityFrameworkCore.Design -v 1.1

Visual Studio

安装包Microsoft.EntityFrameworkCore.Design -v 1.1

在Visual Studio中,检查NuGet Package Manager =>管理解决方案的包,检查所有这些包,无论是否安装在您的解决方案中,如下所示:

实体框架核心 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.InMemory Microsoft.EntityFrameworkCore.Relational Microsoft.EntityFrameworkCore.Sqlite.Core Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Tools

我解决了同样的问题后,检查所有上述软件包已安装。

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.

我只好打了电话

 services.AddEntityFrameworkSqlite().AddDbContext<MovieContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));

在Startup.cs中的ConfigureServices方法中

当我转移到Microsoft.EntityFrameworkCore.SqlServer v3.0.0和Microsoft.EntityFrameworkCore.Tools v3.0.0时,我遇到了这个问题

当我在两个库上都改回v2.2.6时,错误就消失了。这更像是一种变通方法而不是解决方案,但它会让你启动并运行,直到问题得到解决。