Entity Framework - Reuse Complex Type

后端 未结 1 1585
一整个雨季
一整个雨季 2020-12-19 08:30

I have an Entity in Code First Entity framework that currently looks like this:

public class Entity
{
    // snip ...

    public string OriginalDepartment {         


        
相关标签:
1条回答
  • 2020-12-19 09:22

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

    You can also configure your complex types explicitely with model builder

    modelBuilder.ComplexType<Location>();
    

    To customize column names, you should configure them from parent entity configuration

    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 map MyEntity.Current.Queue to myCustomName column

    0 讨论(0)
提交回复
热议问题