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

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


当前回答

将以下代码复制到TodoApi中。来自https://github.com/aspnet/Docs/tree/master/aspnetcore/tutorials/first-web-api/sample/TodoApi的csproj为我解决了类似的问题。

<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>

all可能太多了,但它包含了EntityFrameworkCore

其他回答

我用的是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

首先,我们安装Microsoft.EntityFrameworkCore.SqlServer NuGet包:

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

然后,在导入命名空间之后

using Microsoft.EntityFrameworkCore;

我们添加数据库上下文:

services.AddDbContext<AspDbContext>(options =>
    options.UseSqlServer(config.GetConnectionString("optimumDB")));

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

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

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

哇,这么多答案都没有提到这个Microsoft.EntityFrameworkCore.InMemory包!

将引用添加到这个包中: < PackageReference包括= " Microsoft.EntityFrameworkCore。InMemory" Version="2.2.2" />,你应该很好。

添加 使用Microsoft.EntityFrameworkCore;

手动解决了我的问题

在这里找到

编辑……

为dotnet核心3.1添加

Microsoft.EntityFrameworkCore.SqlServer