还是以这两个表为例子

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();
            }

 

最新文章

  1. 移动Web初级入门
  2. ABP(现代ASP.NET样板开发框架)主题线下交流会(上海)开始报名了!
  3. SQL联合查询(内联、左联、右联、全联)的语法
  4. 多界面开发 、 导航控制器(NavigationController)
  5. mvc异步表单遇到的问题
  6. 用CCRenderTexture和BlendFunc制作游戏教学时使用的黑色覆盖层
  7. asp.net项目中通过Web.config配置文件及文件夹的访问权限---forms
  8. CoFun 1612 单词分组(容斥)
  9. Linux--本地yum库
  10. jQuery 中使用 JSON
  11. Java进阶04 RTTI
  12. Noip 2014酱油记+简要题解
  13. Angular5.0.0新特性
  14. Nginx 11阶段的顺序处理
  15. 【深度好文】多线程之WaitHandle--&gt;派生EventWaitHandle事件构造-》AutoResetEvent、ManualResetEvent
  16. Spring Boot笔记十:IOC控制反转
  17. MYSQL问题解决
  18. linux(ubuntu) 查看系统设备信息 命令
  19. Java编程的逻辑 (38) - 剖析ArrayList
  20. Python3基础 list 元组转成列表

热门文章

  1. python numpy array 与matrix 乘方
  2. kernel-内核抢占
  3. DNS详细解析过程【转】
  4. The C++ Programming Language - Bjarne Stroustrup
  5. sqlalchemy子查询
  6. 第十四节:pandas之merge()合并
  7. 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 4
  8. vue 组件通信传值
  9. 多层gmetad配置
  10. PKI 的组成