如何在EF代码中映射表拆分? [英] How to map table splitting in EF Code First?

查看:89
本文介绍了如何在EF代码中映射表拆分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将表格拆分与EF代码首先映射?描述了EDMX的表拆分,例如此处。它允许将具有1:1关系的两个实体映射到同一个表中。我知道我可以使用实体和复杂类型,但最大的区别是复杂类型不能被懒加载(或根本不加载),这是主要原因

How can I map table splitting with EF Code First? Table splitting for EDMX is described for example here. It allows mapping two entities with 1:1 relation into same table. I know I can do the similar mapping with entity and complex type but the big difference is that complex type can't be lazy loaded (or not loaded at all) which is the main reason for table splitting.

推荐答案

这是我刚刚获得EF 4.1(RC)在Code First中做表拆分的方式。 p>

Here is how I just got EF 4.1 (RC) to do table splitting in Code First.


  1. 定义您的两个实体。确保在两个实体中包含密钥。另外,在指向另一个实体的每个实体中包括导航属性。

  2. 在您的OnModelCreating覆盖中。 。 。
    a。将两个实体映射到同一个表。
    b。创建两个表之间的关系。

  1. Define your two entities. Make sure to include the key in both entities. Also, include navigation properties in each entity pointing to the other entity.
  2. In your OnModelCreating override . . . a. Map both entities to the same table. b. Create the relationship between the two tables.

    modelBuilder.Entity<EntityOne>().ToTable("MySingleTable");
    modelBuilder.Entity<EntityTwo>().ToTable("MySingleTable");

    modelBuilder.Entity<EntityOne>().HasRequired(p => p.NavToEntityTwo).WithRequiredDependent(c => c.NavToEntityOne);


这是为我工作,但意识到鉴于RC的新颖性,我只能看到有限和简单的场景。

This is working for me, but realize that given the newness of the RC I've only been able to look at limited and simple scenarios.

这篇关于如何在EF代码中映射表拆分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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