Object Structure - Complex Reuse

I have an Entity structure in Code First Entity, which currently looks like this:

public class Entity { // snip ... public string OriginalDepartment { get; set; } public string OriginalQueue { get; set; } public string CurrentDepartment { get; set; } public string CurrentQueue { get; set; } } 

I would like to create a Complex Type for these types somehow like this:

 public class Location { public string Department { get; set; } public string Queue { get; set; } } 

I would like to use the same type for Current and Original:

 public Location Original { get; set; } public Location Current { get; set; } 

Is this possible, or do I need to create two complex types CurrentLocation and OriginalLocation ?

 public class OriginalLocation { public string Department { get; set; } public string Queue { get; set; } } public class CurrentLocation { public string Department { get; set; } public string Queue { get; set; } } 
+6
source share
1 answer

It is supported out of the box, you do not need to create two complex types.

You can also customize complex types using the model builder.

 modelBuilder.ComplexType<Location>(); 

To configure column names, you must configure them from the configuration of parent entities

 public class Location { public string Department { get; set; } public string Queue { get; set; } } public class MyEntity { public int Id { get; set; } public Location Original { get; set; } public Location Current { get; set; } } public class MyDbContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.ComplexType<Location>(); modelBuilder.Entity<MyEntity>().Property(x => x.Current.Queue).HasColumnName("myCustomColumnName"); } } 

This will display MyEntity.Current.Queue in myCustomName column

+7
source

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


All Articles