实体框架 - 重用复杂类型 [英] Entity Framework - Reuse Complex Type

查看:120
本文介绍了实体框架 - 重用复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体在代码中第一个实体框架,目前看起来是这样的:

 公共类实体
{
//略...

公共字符串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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆