c# - 在 InMemory 单元测试中运行存储过程

标签 c# entity-framework asp.net-core .net-core .net-core-2.0

如何在内存数据库中运行存储过程?我正在尝试寻找此功能。

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();
}

最佳答案

简短回答;内存提供程序无法做到这一点。

我也遇到了同样的问题;我已经在单元测试中存储了过程,我需要模拟结果。在搜索了一个库来执行此操作并发现没有一个库可以完成这三项任务(FromSqlExecuteSqlCommand 和查询)后,我编写了自己的库:EntityFrameworkCore.Testing 。它使用内存提供程序来完成大多数事情,并为它无法完成的部分提供模拟。

关于c# - 在 InMemory 单元测试中运行存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266518/

相关文章:

c# - 依赖属性或普通属性

linq to entities 无法识别方法 System.Collections.Generic.List

c# - 在 ASP.NET 5/MVC 6 中使用 IDistributedCache 和 Redis - 类型或命名空间 'Microsoft.Caching' 不存在

linux - 在 Linux 上的 AWS Elastic Beanstalk 中部署多个 ASP.NET Core 应用程序(使用 Kestrel 服务器)

razor - 如何使用没有 Controller 的 Razor Pages 填充下拉列表

c# - 当前上下文中不存在名称 'File'

c# - IHttpHandler : Speeding up performance with parallelism?

C#:枚举值可以保存为设置吗?

c# - 在没有 DBSet 的情况下使用 c# linq 执行 SQL Server 存储过程

c# - 模拟 Entity Framework 给我对象引用未设置为对象的实例