【发布时间】:2019-08-11 11:40:50
【问题描述】:
如何在内存数据库中运行存储过程?我正在尝试搜索此功能。
https://docs.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();
}
【问题讨论】:
-
底线是 SProcs 是服务器的东西,而不是 EF 的东西。您可以通过 C# 中的集成测试或直接在服务器上使用集成测试来测试存储过程。
-
试图设置多个内存数据库项目,所以我没有在物理位置进行测试,我们需要为我们的测试运行多个拆卸和设置种子操作
-
您必须意识到实体框架不是 SQL Server。它是一个可以与许多不同的数据库后端进行通信的框架。有MS-SQL、MySql,或者你已经找到了InMemory。您的代码被翻译成后端需要的任何“语言”。 SProcs 与 EF 无关。
-
无论如何,您不应该使用存储过程。在代码中进行测试设置。如果您需要共享它,您可以创建一个帮助类或在 xUnit 中使用
IClassFixture之类的东西。
标签: c# entity-framework asp.net-core .net-core .net-core-2.0