asp.net-mvc 为什么我不能在 DbContextOptionsBuilder 上调用 UseInMemoryDatabase 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48061096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I call the UseInMemoryDatabase method on DbContextOptionsBuilder?
提问by Yusuf Cum
First off, I can't use SQL Lite. Secondly the code below is giving me:
首先,我不能使用 SQL Lite。其次,下面的代码给了我:
Error CS1061 'DbContextOptionsBuilder' does not contain a definition for 'UseInMemoryDatabase' and no extension method 'UseInMemoryDatabase' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)
错误 CS1061“DbContextOptionsBuilder”不包含“UseInMemoryDatabase”的定义,并且找不到接受“DbContextOptionsBuilder”类型的第一个参数的扩展方法“UseInMemoryDatabase”(您是否缺少 using 指令或程序集引用?)
The code:
编码:
var options = new DbContextOptionsBuilder<ProductContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
var context = new ProductContext(options);
Context
语境
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; }
}
}
My project CSPROJ file
我的项目 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>
The exact problem is that the method is just not available. I don't seem to understand why. I require enlightenment on this issue.
确切的问题是该方法不可用。我似乎不明白为什么。我需要对这个问题的启示。
回答by Tetsuya Yamamoto
According to EF Core: Testing with InMemoryreference, you need to add the Microsoft.EntityFrameworkCore.InMemorypackage to use UseInMemoryDatabase()extension method with DbContextOptionsBuilder:
根据EF Core: Testing with InMemory参考,您需要添加Microsoft.EntityFrameworkCore.InMemory包以使用UseInMemoryDatabase()扩展方法DbContextOptionsBuilder:
Install-Package Microsoft.EntityFrameworkCore.InMemory
Afterwards, you can follow example given in "Writing tests" section like this:
之后,您可以按照“编写测试”部分中给出的示例进行操作,如下所示:
var options = new DbContextOptionsBuilder<ProductContext>().UseInMemoryDatabase(databaseName: "database_name").Options;
using (var context = new ProductContext(options))
{
// add service here
}
回答by Felipe Augusto
You need it to use UseInMemoryDatabase
你需要它来使用 UseInMemoryDatabase
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0" />
</ItemGroup>
回答by Wen Qin Yap
Check in your NuGet Package Manager => Manage Packages for Solution, check all this packages, whether got installedin your solution or not, as below:
检查您的NuGet 包管理器 => 管理解决方案包,检查所有这些包,无论是否已安装在您的解决方案中,如下所示:
- EntityFrameworkCore
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.InMemory
- Microsoft.EntityFrameworkCore.Relational
- Microsoft.EntityFrameworkCore.Sqlite.Core
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
- 实体框架核心
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.InMemory
- Microsoft.EntityFrameworkCore.Relational
- Microsoft.EntityFrameworkCore.Sqlite.Core
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
I solved the same issues after check all the above packages been installed.
在检查以上所有软件包都已安装后,我解决了同样的问题。

