2017-09-02 35 views
2

考慮一個具有代表客戶的徽標中的Resource對象Customer實體:HOWTO地圖的值對象2.0

public class Customer 
{ 
    public Guid Id { get; set; } 

    public string CompanyName { get; set; } 

    public Resource Logo { get; set; } 
} 

public class Resource 
{ 
    public string Uri { get; set; } 

    public string Name { get; set; } 
} 

這是我迄今爲止嘗試過,但得到的錯誤,因爲標誌是一個複雜的對象:

var customer = modelBuilder.Entity<Customer>().ToTable("Customers"); 
customer.HasKey(c => c.Id); 
customer.Property(c => c.CompanyName).HasColumnName("Company"); 
customer.Property(c => c.Logo); 

我怎麼能存儲資源與EF核心2.0爲值對象客戶表?

回答

6

如果你想分享你可以簡單地定義附屬實體的同桌:

modelBuilder.Entity<Customer>().OwnsOne(c => c.Logo); 

按照慣例,將只使用一個表。

+0

這就是我一直在尋找!謝謝! (可以在4分鐘內接受答案) –

0

閱讀更多關於它here那裏有一個爲例
搜索與CTRL + F由「擁有的實體和表拆分」