我在Code First Entity框架中有一个实体,目前看起来像这样:
public class Entity
{
// snip ...
public string OriginalDepartment { get; set; }
public string OriginalQueue { get; set; }
public string CurrentDepartment { get; set; }
public string CurrentQueue { get; set; }
}
我想为这些类型创建复杂类型:
public class Location
{
public string Department { get; set; }
public string Queue { get; set; }
}
我想对Current和Original使用相同的类型:
public Location Original { get; set; }
public Location Current { get; set; }
这是可能的,还是我需要创建两个复杂类型CurrentLocation
和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; }
}
答案 0 :(得分:8)
它支持开箱即用,您不需要创建两个复杂类型。
您还可以使用模型构建器
明确配置复杂类型modelBuilder.ComplexType<Location>();
要自定义列名称,您应该从父实体配置
配置它们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");
}
}
这会将MyEntity.Current.Queue
映射到myCustomName
列