Asp.net-Core-Mvc

在 Entity Framework Core 中使用 [ComplexType]

  • August 9, 2015

我在 EF Core 數據模型中使用我自己的類的屬性。

public class Currency
{
   public string Code { get; set; }
   public string Symbol { get; set; }
   public string Format { get; set; }
}

[ComplexType]
public class Money
{
   public int? CurrencyID { get; set; }
   public virtual Currency Currency { get; set; }
   public double? Amount { get; set; }
}

public class Rate
{
   public int ID { get; set; }
   public Money Price = new Money();
}

我的問題是,當我嘗試創建遷移時,EF Core 報告錯誤。

Microsoft.Data.Entity.Metadata.ModelItemNotFoundException: The entity type 'RentABike.Models.Money' requires a key to be defined.

如果我聲明一個鍵,則會為“Money”創建一個單獨的表,這不是我想要的。

有什麼方法可以在 EF Core 中使用 ComplexType 並將其全部放入一個表中?

對複雜類型的支持目前正在積壓中<https://github.com/aspnet/EntityFramework/issues/246>

引用自:https://stackoverflow.com/questions/31906950