我有以下例外:
我用的是. NET 6(我仍然有错误),并认为这可能是与我在解决方案中使用的nugets的一些冲突。事实证明,即使在更新到. NET 7后,当我运行测试时,错误仍然存在。为了测试,我使用MSTest Framework和内存数据库(sqlite)进行集成测试,执行await context.Database.EnsureCreatedAsync();
行出错,测试类如下:
public class SQLiteDatabaseContextFactory : IDisposable
{
private DbConnection _connection;
private DbContextOptions<DataContext> CreateOptions()
{
return new DbContextOptionsBuilder<DataContext>()
.UseSqlite(_connection).Options;
}
public DataContext CreateContext()
{
if (_connection == null)
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
var options = CreateOptions();
using var context = new DataContext(options);
context.Database.EnsureCreated();
}
return new DataContext(CreateOptions());
}
public void Dispose()
{
if (_connection != null)
{
_connection.Dispose();
_connection = null;
}
}
}
以及:
[TestClass]
public class SQLiteIntegrationTests
{
[TestMethod]
public async Task TestMethod_UsingSqliteInMemoryProvider_Success()
{
using var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<DataContext>()
.UseSqlite(connection) // Set the connection explicitly, so it won't be closed automatically by EF
.Options;
// Create the dabase schema
// You can use MigrateAsync if you use Migrations
using (var context = new DataContext(options))
{
//await context.Database.MigrateAsync();
await context.Database.EnsureCreatedAsync();
} // The connection is not closed, so the database still exists
using (var context = new DataContext(options))
{
var user = new ManualClassifier()
{
FirstName = "First",
LastName = "Last",
Email = "example@gmail.com",
Username = "firstlast123",
PasswordHash = "5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf",
PasswordSalt = "5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf"
};
context.ManualClassifiers.Add(user);
await context.SaveChangesAsync();
}
using (var context = new DataContext(options))
{
var count = await context.ManualClassifiers.CountAsync();
Assert.AreEqual(1, count);
var u = await context.ManualClassifiers.FirstOrDefaultAsync(user => user.Email == "example@gmail.com");
Assert.IsNotNull(u);
}
}
}
编辑:完整错误如下:
运行测试的项目的. csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.13" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VSC.Repo\VSC.Repo.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
</ItemGroup>
</Project>
dbcontext类库. csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
</ItemGroup>
</Project>
任何帮助弄清楚正在发生的事情将非常感谢。老实说,我不知道是什么原因造成的。
1条答案
按热度按时间kpbpu0081#
看起来很像软件包版本不匹配(在我的实践中,这是此类错误最常见的来源)。将
Microsoft.EntityFrameworkCore.Sqlite
更新到最新的第7个版本(我猜EF项目使用该版本),以匹配测试解决方案中EF Core软件包的主要版本。