为什么我不能在DbContextOptionsBuilder上调用UseInMemoryDatabase方法?

Yus*_*Cum 20 asp.net-mvc asp.net-mvc-4

首先,我不能使用SQL Lite.其次,下面的代码给了我:

Error   CS1061  'DbContextOptionsBuilder<ProductContext>' does not contain a definition for 'UseInMemoryDatabase' and no extension method 'UseInMemoryDatabase' accepting a first argument of type 'DbContextOptionsBuilder<ProductContext>' could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

代码:

 var options = new DbContextOptionsBuilder<ProductContext>().UseInMemoryDatabase                                  (Guid.NewGuid().ToString())
                              .Options;
            var context = new ProductContext(options);
Run Code Online (Sandbox Code Playgroud)

上下文

using Memory.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Memory.Data
{
    public class ProductContext : DbContext
    {
        public ProductContext(DbContextOptions<ProductContext> options) : base(options)
        {

        }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的项目CSPROJ文件

<ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.5" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.6" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.3" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.3" />
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

确切的问题是该方法不可用.我似乎不明白为什么.我需要在这个问题上得到启发.

Tet*_*oto 56

根据EF Core:使用InMemory测试参考,您需要添加Microsoft.EntityFrameworkCore.InMemory包以使用UseInMemoryDatabase()扩展方法DbContextOptionsBuilder:

Install-Package Microsoft.EntityFrameworkCore.InMemory
Run Code Online (Sandbox Code Playgroud)

之后,您可以按照"编写测试"部分中给出的示例进行操作,如下所示:

var options = new DbContextOptionsBuilder<ProductContext>().UseInMemoryDatabase(databaseName: "database_name").Options;

using (var context = new ProductContext(options))
{
    // add service here
}
Run Code Online (Sandbox Code Playgroud)

  • cli 命令是:`dotnet add package Microsoft.EntityFrameworkCore.InMemory` (9认同)

Sun*_*est 15

Visual Studio 2019:通过控制台...

工具(菜单)-> NuGet 包管理器->包管理器控制台,然后键入dotnet add package Microsoft.EntityFrameworkCore.InMemory 图像

或者通过使用包管理器...

工具(菜单)-> NuGet 包管理器->管理解决方案的 NuGet 包-> NuGet(打开的选项卡)-> 搜索“内存”-> 选择 Microsoft.EntityFrameworkCore.InMemory-> 选中项目->安装框(按钮) 图像

Visual Studio CODE:通过终端...

在屏幕底部,选择终端(选项卡),然后键入dotnet add package Microsoft.EntityFrameworkCore.InMemory

图像


Sai*_*der 12

在 Mac 中,在项目目录中打开终端,或者在Visual Studio中右键单击项目 ->工具->在终端中打开

在终端中通过以下命令安装包->

dotnet add package Microsoft.EntityFrameworkCore.InMemory
Run Code Online (Sandbox Code Playgroud)


Fel*_*sto 8

你需要它来使用 UseInMemoryDatabase

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)