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

Errors compiling optimized DbContext that contains fixed length property #2974

Closed
miegir opened this issue Nov 21, 2023 · 8 comments
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@miegir
Copy link

miegir commented Nov 21, 2023

Steps to reproduce:

  1. In an empty folder, create a new console application: dotnet ef console
  2. Replace Program.cs with the following code:
using Microsoft.EntityFrameworkCore;

Console.WriteLine("Hello world!");

public class Entity
{
    public int Id { get; set; }
    public string? Name { get; set; }
}

public class ApplicationDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Entity>().Property(t => t.Name).HasMaxLength(10).IsFixedLength();
    }
}
  1. Execute dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
  2. Execute dotnet add package Microsoft.EntityFrameworkCore.Design
  3. Execute dotnet new tool-manifest
  4. Execute dotnet tool install dotnet-ef
  5. Execute dotnet ef dbcontext optimize
  6. Execute dotnet build

Expected behavior: the application will compile.

Actual behavior: compilation errors:

D:\ghbug\CompiledModels\EntityEntityType.cs(58,78): error CS0117: 'NpgsqlCharacterStringTypeMapping' does not contain a definition for 'EqualsWithoutTrailingWhitespace' [D:\ghbug\ghbug.csproj]
D:\ghbug\CompiledModels\EntityEntityType.cs(59,68): error CS0117: 'NpgsqlCharacterStringTypeMapping' does not contain a definition for 'GetHashCodeWithoutTrailingWhitespace' [D:\ghbug\ghbug.csproj]
D:\ghbug\CompiledModels\EntityEntityType.cs(62,78): error CS0117: 'NpgsqlCharacterStringTypeMapping' does not contain a definition for 'EqualsWithoutTrailingWhitespace' [D:\ghbug\ghbug.csproj]
D:\ghbug\CompiledModels\EntityEntityType.cs(63,68): error CS0117: 'NpgsqlCharacterStringTypeMapping' does not contain a definition for 'GetHashCodeWithoutTrailingWhitespace' [D:\ghbug\ghbug.csproj]
D:\ghbug\CompiledModels\EntityEntityType.cs(66,78): error CS0117: 'NpgsqlCharacterStringTypeMapping' does not contain a definition for 'EqualsWithoutTrailingWhitespace' [D:\ghbug\ghbug.csproj]
D:\ghbug\CompiledModels\EntityEntityType.cs(67,68): error CS0117: 'NpgsqlCharacterStringTypeMapping' does not contain a definition for 'GetHashCodeWithoutTrailingWhitespace' [D:\ghbug\ghbug.csproj]

This technique worked with Npgsql.EntityFrameworkCore.PostgreSQL version 7.0 but stopped working with 8.0 in which these methods were removed.

@molyviatis
Copy link

molyviatis commented Dec 29, 2023

I get the same error and also "'NpgsqlCharacterStringTypeMapping' does not contain a definition for 'EqualsWithoutTrailingWhitespace'"

@miegir
Copy link
Author

miegir commented Dec 30, 2023

I'm not sure whether the authors ignoring us, but meanwhile I have the following "polyfill" that can be added to the project to resolve the problem: https://gist.github.com/miegir/63961ebdc117ed74e1dd79a5f5afd190.

@roji
Copy link
Member

roji commented Dec 31, 2023

@miegir definitely not ignoring you, but there's a massive amount of work going on. I'll try to get to this for 8.0.1 or 8.0.2 at the latest.

@molyviatis
Copy link

Hi @roji, not sure if this helps but it seems to occur on properties that have IsFixedLength", true

@chriscameron-vertexinc
Copy link

I'm not sure whether the authors ignoring us, but meanwhile I have the following "polyfill" that can be added to the project to resolve the problem: https://gist.github.com/miegir/63961ebdc117ed74e1dd79a5f5afd190.

Thank you so much! I placed the class in the same namespace as my optimized model and it has resolved my errors.

My naïve PR for this problem would be to simply make the EqualsWithoutTrailingWhitespace and GetHashCodeWithoutTrailingWhitespace methods public

@eero-dev
Copy link

eero-dev commented Feb 19, 2024

I ran into this as well on the brand new 8.0.2 just a heads up to anyone trying to make compiled models

@roji roji added this to the 8.0.3 milestone Feb 19, 2024
@roji roji self-assigned this Feb 19, 2024
@roji roji added the bug Something isn't working label Feb 19, 2024
@nibblesnbits
Copy link

+1, Just ran into it today on 8.0.2

roji added a commit that referenced this issue May 11, 2024
@roji
Copy link
Member

roji commented May 11, 2024

Fixed in 51faf6e

@roji roji closed this as completed May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants