简体   繁体   中英

Howto map value object in Entity Framework Core 2.0

Consider a Customer entity that has a Resource object representing the logo of the customer:

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; }
}

This is what I have tried so far but getting an error because Logo is a complex object:

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

How can I store that Resource with EF Core 2.0 as a value object inside the customer table?

If you want to share the same table you could simply define an Owned Entity :

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

By convention it will use just one table.

Read More About it here there have a exemple
Search with CTRL + F by "Owned entities and Table Splitting"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM