Entity Framework Code First -- 延迟加载和预先加载

还是以这两个表为例子

image

 

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();
            }
 
posted @ 2013-09-18 12:46  grkin  阅读(502)  评论(0编辑  收藏  举报