© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
29 replies
flow_state

❔ Help with record types and Ef Core

I have created an entity that uses several value objects. The value objects are all properties on the entity, and they are record types that contain validation login in the ctor, like so
public sealed record DisplayName
{
    private const long MAX_LENGTH = 255;
    public string Value { get; init; }

    public DisplayName(string name)
    {
        name ??= string.Empty;
        
        if (name.Length > MAX_LENGTH)
        {
            var error = DomainErrors.Common.MaxLengthExcceded(nameof(DisplayName), nameof(DisplayName), name, MAX_LENGTH);
            throw new DomainException(error);
        }

        Value = name;
    }

    public static implicit operator string(DisplayName name) => name.Value;
    public static implicit operator DisplayName(string name) => new(name);
    public override string ToString() => Value;
}
public sealed record DisplayName
{
    private const long MAX_LENGTH = 255;
    public string Value { get; init; }

    public DisplayName(string name)
    {
        name ??= string.Empty;
        
        if (name.Length > MAX_LENGTH)
        {
            var error = DomainErrors.Common.MaxLengthExcceded(nameof(DisplayName), nameof(DisplayName), name, MAX_LENGTH);
            throw new DomainException(error);
        }

        Value = name;
    }

    public static implicit operator string(DisplayName name) => name.Value;
    public static implicit operator DisplayName(string name) => new(name);
    public override string ToString() => Value;
}

The problem is that I run into this error when trying to build the Asp.net core app to generate a swagger json file
Unhandled exception. System.AggregateException: One or more errors occurred. (No suitable constructor was found for entity type 'DisplayName'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'name' in 'DisplayName(string name)'; cannot bind 'original' in 'DisplayName(DisplayName original)'.)
Unhandled exception. System.AggregateException: One or more errors occurred. (No suitable constructor was found for entity type 'DisplayName'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'name' in 'DisplayName(string name)'; cannot bind 'original' in 'DisplayName(DisplayName original)'.)


Here is a snippet of the ef core configuration for this property
builder.Property(r => r.DisplayName)
    .HasConversion(d => d.Value, d => new DisplayName(d))
    .IsRequired()
    .HasMaxLength(255)
    .HasColumnName("display_name");
builder.Property(r => r.DisplayName)
    .HasConversion(d => d.Value, d => new DisplayName(d))
    .IsRequired()
    .HasMaxLength(255)
    .HasColumnName("display_name");

Note: The entity class itself is a regular class.

Not sure how to proceed from here. Any help is appreciated.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Ef core help
C#CC# / help
10mo ago
Help with EF Core polymorphic associations
C#CC# / help
2y ago
❔ Help with EF Core Linq statement
C#CC# / help
4y ago
✅ Inserting record with one-to-many relationship EF Core
C#CC# / help
3y ago