Efficient EF Core mapping for internal object properties

I have a class that contains some properties. For some architectural reasons, I have an instance of another object in my class.

Simple example

public class MyEntity { public MySubEntity SubEntity {get; set;} } 

To do this, I create a free display, for example:

 builder.ToTable(MyEntity.CONST_TABLE_NAME); builder.HasKey(m => m.Id); builder.Property(m => m.Column1).IsRequired(); builder.Property(m => m.SubEntity.Column2).IsRequired(); 

I cannot integrate all of my subEntity properties into my core entity (my subEntity has its own intelligence). I just want to map my subentity properties, which are NOT stored in a separate table, in the myEntity table.

The last line throws an exception:

 The expression 'm => m.SubEntity.Column2' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'. 

How can I do this mapping?

+5
source share
1 answer

EF Core does not yet support this type of display. It will not be supported in RTF EF Core 1.0 (see My github issue: https://github.com/aspnet/Home/issues/1330 )

As I described in my github problem, I figured out 2 solutions:

1) Create a derived class from my model specifically designed for EF and demonstrate all the properties as simple. It will need more matching when inserting / updating and retrieving from Db. We do not select this option.

2) Create proxy properties. In my example, it looks like this:

 public class MyEntity { private MySubEntity SubEntity {get; set;} public string SubEntityValue { get { return SubEntity.Value; } set { SubEntity.Value = value; } } 

This seems like the best solution (we choose this option).

+3
source

Source: https://habr.com/ru/post/1243864/


All Articles