实体框架 - 重用复杂类型 [英] Entity Framework - Reuse Complex Type
问题描述
我有一个实体在代码中第一个实体框架,目前看起来是这样的:
公共类实体
{
//略...
公共字符串OriginalDepartment {搞定;组; }
公共字符串OriginalQueue {搞定;组; }
公共字符串CurrentDepartment {搞定;组; }
公共字符串CurrentQueue {搞定;组; }
}
我想创建为这些类型是这样的复杂类型:
公共类位置
{
公共字符串系{搞定;组; }
公共字符串队列{搞定;组; }
}
我想使用相同类型的当前和原文:
公共场所原件{搞定;组; }
公共场所目前{搞定;组; }
这是可能的,还是我需要创建两个复杂类型 CurrentLocation
和 OriginalLocation
?
公共类OriginalLocation
{
公共字符串系{搞定;组; }
公共字符串队列{搞定;组; }
}
公共类CurrentLocation
{
公共字符串系{搞定;组; }
公共字符串队列{搞定;组; }
}
有支持出来的中,您不需要创建两个复杂类型。
您还可以与模型构建配置复杂类型明确地
modelBuilder.ComplexType<地点>();
要自定义列名,您应该从父实体配置配置它们
公共类位置
{
公共字符串系{搞定;组; }
公共字符串队列{搞定;组; }
}
公共类myEntity所
{
公众诠释标识{搞定;组; }
公共场所原件{搞定;组; }
公共场所目前{搞定;组; }
}
公共类MyDbContext:的DbContext
{
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
modelBuilder.ComplexType<地点> ;();
modelBuilder.Entity< myEntity所方式>()房产(X => x.Current.Queue).HasColumnName(myCustomColumnName);
}
}
这将映射 myEntity所。 Current.Queue
到 myCustomName
列
I have an Entity in Code First Entity framework that 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 Complex Type for these types as something like this:
public class Location
{
public string Department { get; set; }
public string Queue { get; set; }
}
I'd like to use this same type for both 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; }
}
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
这篇关于实体框架 - 重用复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!