我通过 OwnsOne 配置了内部对象:
builder.OwnsOne(navigationExpression, b => {
b.Property(x => x.Value);
b.Property(x => x.Readonly);
});
我想使用这个对象的属性(值)作为外键。 我试试看:
builder
.HasOne(x => x.Issuer)
.WithMany()
.HasPrincipalKey(x => x.Id)
.HasForeignKey(x => x.IssuerId.Value);
但是我在尝试创建迁移时发现异常
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.ArgumentException: The expression 'x => Convert(x.IssuerId.Value, Object)' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. When specifying multiple properties or fields, use an anonymous type: 't => new { t.MyProperty, t.MyField }'. (Parameter 'memberAccessExpression')
根对象看起来像这样
public class ShareViewModel : ViewModel
{
public OwnableValueViewModel<Guid> IssuerId { get; private set; }
public OwnableValueViewModel<Guid?> RegistrationAuthorityId { get; private set; }
/* .... other properties .... */
public virtual ContractorViewModel Issuer { get; private set; }
public virtual ContractorViewModel? RegistrationAuthority { get; private set; }
和 OwnableValueViewModel
public record OwnableValueViewModel<T>
{
public T? Value { get; init; }
public bool Readonly { get; init; }
}
有办法配置吗?
我找到了这个答案:
Create relationship with a property inside owned Entity 导致错误
但这不适合我,因为我的导航属性放置在根对象(ShareViewModel)中但不在内部对象(OwnableValueViewModel)中