Skip to content

Support value conversion with generated properties #2683

@roji

Description

@roji

EF Core 7.0 introduced support for value convertors on generated properties (docs, dotnet/efcore#11597), but we don't seem to support that.

Make sure it works for all our value generation strategies (identity, HiLo (#2431), sequence).

Code sample
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

ctx.Blogs.Add(new Blog { Name = "foo" });
await ctx.SaveChangesAsync();

ctx.ChangeTracker.Clear();

var blog = ctx.Blogs.Single();
Console.WriteLine(blog.Id.Value);

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(@"Host=localhost;Username=test;Password=test")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .Property(b => b.Id)
            .UseIdentityAlwaysColumn()
            .HasConversion(i => i.Value, v => new BlogId { Value = v });
    }
}

public class Blog
{
    public BlogId Id { get; set; }
    public string? Name { get; set; }
}

public class BlogId
{
    public int Value;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions