还是以这两个表为例子

country包含零个或多个city, 这个外键关系是我后来加上去,原来没有。 然后再用Power Tool逆向, 产生如下代码

   1:  using System.ComponentModel.DataAnnotations.Schema;
   2:  using System.Data.Entity.ModelConfiguration;
   3:   
   4:  namespace EFEntity.Models.Mapping
   5:  {
   6:      public class cityMap : EntityTypeConfiguration<city>
   7:      {
   8:          public cityMap()
   9:          {
  10:              // Primary Key
  11:              this.HasKey(t => t.ID);
  12:   
  13:              // Properties
  14:              this.Property(t => t.Name)
  15:                  .IsRequired()
  16:                  .HasMaxLength(35);
  17:   
  18:              this.Property(t => t.CountryCode)
  19:                  .IsRequired()
  20:                  .HasMaxLength(3);
  21:   
  22:              this.Property(t => t.District)
  23:                  .IsRequired()
  24:                  .HasMaxLength(20);
  25:   
  26:              // Table & Column Mappings
  27:              this.ToTable("city", "world");
  28:              this.Property(t => t.ID).HasColumnName("ID");
  29:              this.Property(t => t.Name).HasColumnName("Name");
  30:              this.Property(t => t.CountryCode).HasColumnName("CountryCode");
  31:              this.Property(t => t.District).HasColumnName("District");
  32:              this.Property(t => t.Population).HasColumnName("Population");
  33:   

34: // Relationships //这里加了一个关系 35: this.HasRequired(t => t.country) //这个指向city 模型的 public virtual country country { get; set; } 36: .WithMany(t => t.cities) //这个指向country模型的  public virtual ICollection<city> cities { get; set; } 37: .HasForeignKey(d => d.CountryCode); //这个指向city模型的public string CountryCode { get; set; }

  38:   
  39:          }
  40:      }
  41:  }

以上是city映射, 下面是country映射

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
 
namespace EFEntity.Models.Mapping
{
    public class countryMap : EntityTypeConfiguration<country>
    {
        public countryMap()
        {
            // Primary Key
            this.HasKey(t => t.Code);
 
            // Properties
            this.Property(t => t.Code)
                .IsRequired()
                .HasMaxLength(3);
 
            this.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(52);
 
            this.Property(t => t.Continent)
                .IsRequired()
                .HasMaxLength(65532);
 
            this.Property(t => t.Region)
                .IsRequired()
                .HasMaxLength(26);
 
            this.Property(t => t.LocalName)
                .IsRequired()
                .HasMaxLength(45);
 
            this.Property(t => t.GovernmentForm)
                .IsRequired()
                .HasMaxLength(45);
 
            this.Property(t => t.HeadOfState)
                .HasMaxLength(60);
 
            this.Property(t => t.Code2)
                .IsRequired()
                .HasMaxLength(2);
 
            // Table & Column Mappings
            this.ToTable("country", "world");
            this.Property(t => t.Code).HasColumnName("Code");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.Continent).HasColumnName("Continent");
            this.Property(t => t.Region).HasColumnName("Region");
            this.Property(t => t.SurfaceArea).HasColumnName("SurfaceArea");
            this.Property(t => t.IndepYear).HasColumnName("IndepYear");
            this.Property(t => t.Population).HasColumnName("Population");
            this.Property(t => t.LifeExpectancy).HasColumnName("LifeExpectancy");
            this.Property(t => t.GNP).HasColumnName("GNP");
            this.Property(t => t.GNPOld).HasColumnName("GNPOld");
            this.Property(t => t.LocalName).HasColumnName("LocalName");
            this.Property(t => t.GovernmentForm).HasColumnName("GovernmentForm");
            this.Property(t => t.HeadOfState).HasColumnName("HeadOfState");
            this.Property(t => t.Capital).HasColumnName("Capital");
            this.Property(t => t.Code2).HasColumnName("Code2");
        }
    }
}
 

测试代码--延迟加载:

 using (var context = new worldContext())
            {
                var country = from d in context.countries
                            where d.Name == "United States"
                            select d;
                var countryUS = country.Single();
                if (countryUS == null) return;
                foreach (var city in countryUS.cities)
                {
                    Console.WriteLine(city.Name);
                }
                Console.Read();

测试代码--预先加载:

            using (var context = new worldContext())
            {
                var allCountries = context.countries.Include(p => p.cities);
                foreach (var country in allCountries)
                {
                    Console.WriteLine(country.Name);
                    foreach (var city in country.cities)
                    {
                        Console.WriteLine("__" + city.Name);
                    }
                }
                Console.Read();
            }

 

Entity Framework Code First -- 延迟加载和预先加载的更多相关文章

  1. Entity Framework入门教程(8)---预先加载、延迟加载、显示加载

    1.预先加载 预先加载:在对一种类型的实体进行查询时,将相关的实体作为查询的一部分一起加载.预先加载可以使用Include()方法实现. 1.加载一个相关实体类型 栗子:使用Include()方法从数 ...

  2. Entity Framework Code First实体关联数据加载

    在项目过程中,两个实体数据之间在往往并非完全独立的,而是存在一定的关联关系,如一对一.一对多及多对多等关联.存在关联关系的实体,经常根据一个实体的实例来查询获取与之关联的另外实体的实例. Entity ...

  3. EF 延迟加载和预先加载

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨延迟加载和预先加载 Entity Frame ...

  4. Entity Framework关联实体的三种加载方法

    推荐文章 EF性能之关联加载 总结很好 一:介绍三种加载方式 Entity Framework作为一个优秀的ORM框架,它使得操作数据库就像操作内存中的数据一样,但是这种抽象是有性能代价的,故鱼和熊掌 ...

  5. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  6. Entity Framework Code First学习系列

    Entity Framework Code First学习系列目录 Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity ...

  7. Entity Framework Code First关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  8. Entity Framework Code First主外键关系映射约定

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  9. Entity Framework Code First关系映射约定【l转发】

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

随机推荐

  1. copy.c实现

    #cat copy.c #include <stdio.h> #include <stdlib.h> #include <string.h> int copyFil ...

  2. Enable ssh root login in Solaris

    1. Change the file /etc/ssh/sshd_config with PermitRootLogin yes to replace PermitRootLogin no 2. re ...

  3. for循环提高内存访问效率的做法

    今天写程序的时候突然想到一点,记录一下: 计算机内存地址是线性排列组织的,而利用for循环对高维数组结构进行遍历处理的时候,要保证最内层for循环遍历的是高维数组的最低维度,这样可以最大化利用CPU的 ...

  4. 关于PyQt5,在pycharm上的安装步骤及使用技巧

    前序 之前学习了一款GUI图形界面设计的Tkinter库,但是经大佬的介绍,PyQT5全宇宙最强,一脸的苦笑 毫不犹豫的选择转战PyQT5,在学习之前需要先安装一些必须程序,在一番查阅后,发现PyQt ...

  5. ZOJ 3687 The Review Plan I

    The Review Plan I Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on ZJU. Origi ...

  6. 树网的核(codevs 1167)

    题目描述 Description [问题描述]设 T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边带有正整数的权,我们称T 为树网(treenetwork),其中V, E分别表 ...

  7. 2.3. Configuring sudo Access-RedHat

    https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux_OpenStack_Platform/2/html/Get ...

  8. hdu_1232_畅通工程_201403091018

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. hdu_1003_Max Sum_201311271630

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  10. 递归算法详细分析->C

    C通过运行时堆栈支持递归函数的实现.递归函数就是直接或间接调用自身的函数.     许多教科书都把计算机阶乘和菲波那契数列用来说明递归,非常不幸我们可爱的著名的老潭老师的<C语言程序设计> ...