如何在内存数据库中运行存储过程?我正在尝试寻找此功能。
https://learn.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory
这是用于设置:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFProviders.InMemory;Trusted_Connection=True;ConnectRetryCount=0");
}
}
var options = new DbContextOptionsBuilder<BloggingContext>()
.UseInMemoryDatabase(databaseName: "Find_searches_url")
.Options;
// Insert seed data into the database using one instance of the context
using (var context = new BloggingContext(options))
{
context.Blogs.Add(new Blog { Url = "http://sample.com/cats" });
context.Blogs.Add(new Blog { Url = "http://sample.com/catfish" });
context.Blogs.Add(new Blog { Url = "http://sample.com/dogs" });
context.SaveChanges();
}
最佳答案
简短回答;内存提供程序无法做到这一点。
我也遇到了同样的问题;我已经在单元测试中存储了过程,我需要模拟结果。在搜索了一个库来执行此操作并发现没有一个库可以完成这三项任务(FromSql
、ExecuteSqlCommand
和查询)后,我编写了自己的库:EntityFrameworkCore.Testing 。它使用内存提供程序来完成大多数事情,并为它无法完成的部分提供模拟。
关于c# - 在 InMemory 单元测试中运行存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266518/