Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnableNullChecks is cached unexpectedly? #26840

Closed
gojanpaolo opened this issue Nov 29, 2021 · 1 comment · Fixed by #29057
Closed

EnableNullChecks is cached unexpectedly? #26840

gojanpaolo opened this issue Nov 29, 2021 · 1 comment · Fixed by #29057
Labels
area-in-memory closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@gojanpaolo
Copy link

gojanpaolo commented Nov 29, 2021

The following code passes dotnet test. (Also tried in VS 2022 17.0.0 and same behavior is observed)

using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Xunit;

public class UnitTest1
{
  [Fact]
  public async Task Test1()
  {
    // UNCOMMENT THIS CODE TO FAIL TEST
    // await using var ctx1 = new TestDbContext(
    //   new DbContextOptionsBuilder<TestDbContext>()
    //   .UseInMemoryDatabase(Guid.NewGuid().ToString())
    //   .Options);

    await using var ctx2 = new TestDbContext(
      new DbContextOptionsBuilder<TestDbContext>()
      .UseInMemoryDatabase(Guid.NewGuid().ToString(), _ => _.EnableNullChecks(false))
      .Options);
    await ctx2.Foo.AddAsync(new());
    await ctx2.SaveChangesAsync();
  }
}
public class TestDbContext : DbContext
{
  public TestDbContext(DbContextOptions<TestDbContext> options) : base(options) { }
  public DbSet<Foo> Foo { get; private init; } = null!;
}
public class Foo
{
  public int Id { get; set; }
  public string Bar { get; set; } = null!;
}

csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
  </ItemGroup>
</Project>

But uncommenting the code as described in the code fails dotnet test

$ dotnet test
  Determining projects to restore...
  All projects are up-to-date for restore.
  efcore6 -> C:\Users\gojan\sb\efcore6\bin\Debug\net6.0\efcore6.dll
Test run for C:\Users\gojan\sb\efcore6\bin\Debug\net6.0\efcore6.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.0.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.82]     UnitTest1.Test1 [FAIL]
  Failed UnitTest1.Test1 [378 ms]
  Error Message:
   Microsoft.EntityFrameworkCore.DbUpdateException : Required properties '{'Bar'}' are missing for the instance of entity type 'Foo'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the entity key value.
  Stack Trace:
     at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTable`1.ThrowNullabilityErrorException(IUpdateEntry entry, IList`1 nullabilityErrors)
   at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTable`1.Create(IUpdateEntry entry)
   at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryStore.ExecuteTransaction(IList`1 entries, IDiagnosticsLogger`1 updateLogger)
   at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryDatabase.SaveChangesAsync(IList`1 entries, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at UnitTest1.Test1() in C:\Users\gojan\sb\efcore6\UnitTest1.cs:line 22
   at UnitTest1.Test1() in C:\Users\gojan\sb\efcore6\UnitTest1.cs:line 22
   at UnitTest1.Test1() in C:\Users\gojan\sb\efcore6\UnitTest1.cs:line 22
--- End of stack trace from previous location ---

Failed!  - Failed:     1, Passed:     0, Skipped:     0, Total:     1, Duration: < 1 ms - efcore6.dll (net6.0)

My expected behavior is that EnableNullChecks can be configured differently for each DbContextOptions instance. Thank you


Operating system: Windows 10

@ajcvickers
Copy link
Member

Note for triage: this should be a singleton option and cause a service provider re-build if changed.

@ajcvickers ajcvickers self-assigned this Nov 29, 2021
@ajcvickers ajcvickers added this to the 7.0.0 milestone Dec 1, 2021
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 12, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc2 Sep 14, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-in-memory closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants