flow_state
flow_state
CC#
Created by flow_state on 2/15/2023 in #help
❔ 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.
30 replies
CC#
Created by flow_state on 8/23/2022 in #help
Need Help with creating a JsonSchema
I need to create a JSON-Schema to validate some properties of a json object. I'm using the oneOf property to perform some logic that has to match only one sub-schema. My issue is that while testing, my json file keeps matching multiple entries in the oneOf and I cannot figure out why.
2 replies