我正在创建用于单元测试的SQLite内存数据库:
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
try
{
var options = new DbContextOptionsBuilder<BloggingContext>()
.UseSqlite(connection)
.Options;
// Create the schema in the database
using (var context = new BloggingContext(options))
{
context.Database.EnsureCreated();
}
// Run the test against one instance of the context
using (var context = new BloggingContext(options))
{
var service = new BlogService(context);
service.Add("http://sample.com");
}
// Use a separate instance of the context to verify correct data was saved to database
using (var context = new BloggingContext(options))
{
Assert.AreEqual(1, context.Blogs.Count());
Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
}
}
context.Database.EnsureCreated(); 在执行时抛出异常:Message: Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: 'near "MAX": syntax error'.
在 github issue 中有这样的说法: 问题在于 varchar(max) 是 SqlServer 特定的类型。脚手架不应将其添加为关系型类型,因为这会导致将其传递到其他提供程序的迁移中生成无效的 SQL。
那么,如果我的数据库包含许多 varchar(max) 列,我如何在内存中使用 SQLite 进行单元测试呢?
_offerContext.Set<Aggregate>().FindAsync(id);
时,我会得到异常:typeof(Microsoft.Data.Sqlite.SqliteException): SQLite Error 1: 'no such table: Offers'. 而不是 null。如果我传递的 id 对应的实体存在,它会返回正确的对象。但是我想测试当我找不到实体时的行为。 - Deivydas Voroneckis