efcore
efcore copied to clipboard
Auto-generated compiled model results in an error after entity type rename
Using <EFOptimizeContext>true</EFOptimizeContext> generates confusing errors if the user code that forms the basis of compiled model is changed. For example, starting with this:
Console.WriteLine("Hello, World!");
public class Blog
{
public int Id { get; set; }
}
public class BlogsContext : DbContext
{
public DbSet<Blog> Blogs => Set<Blog>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Data Source=localhost;Database=Two;Integrated Security=True;Trust Server Certificate=True;Connect Retry Count=0")
.LogTo(Console.WriteLine)
.EnableSensitiveDataLogging();
}
And then renaming Blog to BlogX and running build results in:
ConsoleApp1 failed with errors (0.2s)
D:\code\ConsoleApp1\ConsoleApp1\obj\Debug\net8.0\BlogEntityType.g.cs(127,61): error CS0246: The type or namespace name 'Blog' could not be found (are you missing a using directive or an assembly reference?) [D:\code\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj]
Doing a dotnet clean doesn't help:
PS D:\code\ConsoleApp1\ConsoleApp1> dotnet clean
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
Build succeeded in 0.5s
PS D:\code\ConsoleApp1\ConsoleApp1> dotnet build -v m
Restore complete (0.2s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
ConsoleApp1 failed with errors (0.2s)
D:\code\ConsoleApp1\ConsoleApp1\obj\Debug\net8.0\BlogEntityType.g.cs(127,61): error CS0246: The type or namespace name 'Blog' could not be found (are you missing a using directive or an assembly reference?) [D:\code\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj]
Build failed with errors in 1.0s
The only thing that works is manually deleting the obj directory.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EFOptimizeContext>true</EFOptimizeContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0-preview.4.24205.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0-preview.4.24205.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tasks" Version="9.0.0-preview.4.24205.3" />
</ItemGroup>
</Project>