EF核心 - 无效的复杂属性



我希望能够创建一个订单对象,而无需在创建(以db)时刻定义 SomeOptionalType

afaik在数据库中必须是无效的freef键

.net core 2.1/ef core带有后gre sql驱动程序

public class Order
{
    [ForeignKey("SomeOptionalTypeId")]
    public Guid SomeOptionalTypeId { get; set; }
    public SomeType? SomeOptionalType { get; set; }
}
public class SomeType
{
    public Guid Id { get; set; } = Guid.NewGuid();
}

我该如何实现?

如果SomeType? SomeOptionalType是可选的,并且可以是null,则Guid SomeOptionalTypeId也可以是null

Guid更改为Guid?

[ForeignKey("SomeOptionalTypeId")]
public Guid? SomeOptionalTypeId { get; set; }

最新更新