在 .net8 和 EF8 中我收到此错误:
找不到实体类型“DatabaseTableId”合适的构造函数。 以下构造函数具有无法绑定的参数 实体类型的属性: 无法在“DatabaseTableId(long value, bool isInternalError)”中绑定“value”、“isInternalError” 无法绑定“DatabaseTableId(DatabaseTableId value)”中的“value”请注意,只有映射的属性可以绑定到构造函数 参数。导航至相关实体,包括对 拥有的类型,无法绑定。
使用EF从数据库获取值时出现错误
在以下代码中:
public async Task<PetitionState> GetActualStateOfPetitionAsync(string PetitionId)
{
try
{
ValueObjectExtensions.ThrowInternalArgumentNullExceptionIfNull(PetitionId);
string? state = await _dbContext.WebServicePetitionState
.Where(e => e.Id == PetitionId)
.Select(e => e.State)
.FirstOrDefaultAsync();
return new PetitionState(state, true);
}
catch
{
throw;
}
}
在 dbContext 类中:
public DbSet<WebServicePetitionStateEntity> WebServicePetitionState{ get; set; }
modelBuilder.Entity<WebServicePetitionStateEntity>()
.ToTable("WEBSERVICE_PETITION_STATE", schema: "QUESTION")
.HasKey(e => e.Id);
实体:
public class WebServicePetitionStateEntity
{
[Key]
[Column("ID")]
public required string Id{ get; set; }
[Column("RESPONSE_DATE")]
public DateTime? ResponseDate{ get; set; }
[Column("STATE")]
public required string State{ get; set; }
}
“DatabaseTableId”是我创建的 ValueObject,但在此函数中未使用
namespace Domain.ValueObjects.Common;
/// <summary>
/// Represents a database table ID value object.
/// </summary>
public class DatabaseTableId : ValueObject<long>
{
/// <summary>
/// Gets the value of the database table ID.
/// </summary>
public override long Value { get; }
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseTableId"/> class with the specified value.
/// </summary>
/// <param name="value">The value of the database table ID.</param>
public DatabaseTableId(long value, bool isInternalError = false)
{
Value = value;
ValidateAndThrow(isInternalError);
}
public DatabaseTableId() { }
protected override void ValidateInternal()
{
if (Value <= 0)
{
throw new ArgumentException($"{nameof(DatabaseTableId)} cannot be less than or equal to 0.");
}
if (Value > long.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(DatabaseTableId), $"{nameof(DatabaseTableId)}: {Value} cannot have a length bigger than {long.MaxValue}.");
}
if (Value.ToString().Length > ValueObjectConstants.MaxIdLength)
{
throw new ArgumentOutOfRangeException(nameof(DatabaseTableId), $"{nameof(DatabaseTableId)}: {Value} cannot have more than {ValueObjectConstants.MaxIdLength} digits.");
}
}
}
和值对象
using Domain.Interfaces;
namespace Domain.Exceptions;
/// <summary>
/// Base class for all value objects that can be validated.
/// </summary>
/// <typeparam name="TValue">The type of the value of the value object.</typeparam>
public abstract class ValueObject<TValue>
{
public abstract TValue Value { get; }
/// <summary>
/// Validates the value object and throws an exception if there are any validation errors, if isInternalException is true, throws a "InternalValidationExcepction" exception
/// </summary>
public void ValidateAndThrow(bool isInternalException = false)
{
try
{
ValidateInternal();
}
catch (Exception ex)
{
if (isInternalException)
{
throw new InternalValidationException(ex.Message);
}
else
{
throw;
}
}
}
/// <summary>
/// Equality operator for value objects.
/// </summary>
/// <param name="left">The left value object.</param>
/// <param name="right">The right value object.</param>
/// <returns>True if the value objects are equal, false otherwise.</returns>
public static bool operator ==(ValueObject<TValue>? left, ValueObject<TValue>? right)
{
if (ReferenceEquals(left, right))
{
return true;
}
if (left is null || right is null)
{
return false;
}
if (left.Value is null || right.Value is null)
{
return false;
}
return left.Value.Equals(right.Value);
}
/// <summary>
/// Inequality operator for value objects.
/// </summary>
/// <param name="left">The left value object.</param>
/// <param name="right">The right value object.</param>
/// <returns>True if the value objects are not equal, false otherwise.</returns>
public static bool operator !=(ValueObject<TValue>? left, ValueObject<TValue>? right)
{
return !(left == right);
}
/// <summary>
/// Determines whether the current value object is equal to another object.
/// </summary>
/// <param name="obj">The object to compare with the current value object.</param>
/// <returns>True if the current value object is equal to the other object, false otherwise.</returns>
public override bool Equals(object? obj)
{
if (obj is ValueObject<TValue> other)
{
return Value != null && other.Value != null && Value.Equals(other.Value);
}
return false;
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>A hash code for the current value object.</returns>
public override int GetHashCode() => Value?.GetHashCode() ?? 0;
/// <summary>
/// Returns a string that represents the current value object.
/// </summary>
/// <returns>A string that represents the current value object.</returns>
public override string ToString() => Value?.ToString() ?? "";
/// <summary>
/// Validates the value object and returns a list of validation errors.
/// </summary>
/// <returns>A list of validation errors.</returns>
protected abstract void ValidateInternal();
}
我解决了错误,在 DatabaseTableId 值对象中添加:
public DatabaseTableId() { }
。
此后此值对象不再出现此错误,但其他值对象出现此错误,修复后,它可以正常工作。