小编Ste*_*son的帖子

在单元测试中使用EF Core SqlLite时防止跟踪问题

我正在编写单元测试来测试更新EF核心实体的控制器操作.

我使用的是SQLLite,而不是嘲笑.

我像这样设置我的数据库:

        internal static ApplicationDbContext GetInMemoryApplicationIdentityContext()
    {
        var connection = new SqliteConnection("DataSource=:memory:");
        connection.Open();

        var options = new DbContextOptionsBuilder<ApplicationDbContext>()
                .UseSqlite(connection)
                .Options;

        var context = new ApplicationDbContext(options);
        context.Database.EnsureCreated();

        return context;
Run Code Online (Sandbox Code Playgroud)

然后将实体添加到数据库,如下所示:

        private DiaryEntriesController _controller;
    private ApplicationDbContext _context;

    [SetUp]
    public void SetUp()
    {
        _context = TestHelperMethods.GetInMemoryApplicationIdentityContext();
        _controller = new DiaryEntriesController(_context);
    }

    [Test]
    [Ignore("http://stackoverflow.com/questions/42138960/preventing-tracking-issues-when-using-ef-core-sqllite-in-unit-tests")]
    public async Task EditPost_WhenValid_EditsDiaryEntry()
    {
        // Arrange
        var diaryEntry = new DiaryEntry
        {
            ID = 1,
            Project = new Project { ID = 1, Name = "Name", Description = …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core asp.net-core

10
推荐指数
1
解决办法
2961
查看次数