Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 827
Martin Brandl Avatar asked Dec 07 '22 17:12

Martin Brandl


1 Answers

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.

like image 139
Federico Dipuma Avatar answered Dec 19 '22 01:12

Federico Dipuma