程式碼優先複雜型別

複雜型別允許你將資料庫表的選定欄位對映到主型別的子型別的單個型別。

[ComplexType]
public class Address
{
    public string Street { get; set; }
    public string Street_2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

然後,可以在多個實體型別中使用此複雜型別。它甚至可以在同一實體型別中多次使用。

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    ...
    public Address ShippingAddress { get; set; }
    public Address BillingAddress { get; set; }
}

然後,此實體型別將儲存在資料庫中的表中,該表看起來像這樣。

StackOverflow 文件

當然,在這種情況下,1:n 關聯(客戶地址)將是首選模型,但該示例顯示瞭如何使用複雜型別。