Map one instance of many models to one property in EF Core

.net-core c# entity-framework entity-framework-core

Question

Say I have the following DB structure:

asset:                         metadata_text:
 id | type | source             asset_id | font_size | font_color | font_name
----|------|-----------        ----------|-----------|------------|-----------
 0  | vid  | http://...         2        | 24        | #FF8800    | Arial
 1  | img  | http://...                            ...
 2  | text | null
      ...

metadata_vid:                                 metadata_img:
 asset_id | width | height | length            asset_id | width | height 
----------|-------|--------|--------          ----------|-------|--------
 0        | 1920  | 1080   | 1303              1        | 400   | 400
                ...                                      ...

I have assets with certain types, and each type has a metadata table associated with it. An asset only has one entry in one of the metadata tables. How would I map an asset's metadata to a single property in EF core? I would like my Asset entity to look like

public class Asset
{
    public long Id { get; set; }
    public string Type { get; set; }
    public string Source { get; set; }
    public object Metadata { get; set; }  // populate this with the entry from the relevant table
}

Is there any way to do this using EF Core? I understand that you can do type inheritance however this doesn't help my situation as it requires all metadata fields for all types to be in the same table as the assets. Any help is greatly appreciated!

1
2
2/4/2020 7:56:29 PM

Popular Answer

Unfortunately table per type (TPT) inheritance is not supported in EF Core yet and this sounds like a great use case for it: GitHub TPT issue #2266

What would be the disadvantage of having a separate property for each?

There may then be a way to use AutoMapper to map all the properties to the properties of a single class within a model class. Or at least create your own mappings.

2
2/4/2020 10:51:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow