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

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

问题描述

如何使用 EF Code First 映射表拆分?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 中进行表拆分的方法.

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

  1. 定义您的两个实体.确保在两个实体中都包含密钥.此外,在每个实体中包含指向另一个实体的导航属性.
  2. 在您的 OnModelCreating 覆盖中...一种.将两个实体映射到同一个表.湾创建两个表之间的关系.

  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 Code First 中映射表拆分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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