EF中的1:0或1:1关系以及1:n关系_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > EF中的1:0或1:1关系以及1:n关系

EF中的1:0或1:1关系以及1:n关系

 2014/12/5 15:09:38  haiziguo  程序员俱乐部  我要评论(0)
  • 摘要:先给出1:0关系User表包括用户名和密码publicclassUser{publicintID{get;set;}publicstringUserName{get;set;}publicstringPasswd{get;set;}publicvirtualUserInfoUserInfo{get;set;}}UserInfo表包括用户信息publicclassUserInfo{publicintID{get;set;}publicstringName{get;set;
  • 标签:关系

先给出1:0关系

User表包括用户名和密码

public class User
    {
       public int ID { get; set; }

       public string UserName { get; set; }

       public string Passwd { get; set; }

       public virtual UserInfo UserInfo { get; set; }

    }

UserInfo表包括用户信息

public class UserInfo
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public string  Phone { get; set; }

        public virtual User User { get; set; }

        
    }

image

UseInfos中的ID即是主键也是外键

再给出1:n关系

Video表包括视频的信息

public class Video
  {
      public int ID { get; set; }

      public string  Title { get; set; }
      
      public virtual ICollection<VideoRecord> Records { get; set; }
  }

Record表包括视频的观看记录

public class VideoRecord
    {
       public int ID { get; set; }

       public DateTime CrateAt { get; set; }

       public string IP { get; set; }

       public Video Video { get; set; }
    }

image

下面是关系的设置。

modelBuilder.Entity<User>().ToTable("Users");
            modelBuilder.Entity<User>().HasKey(u => u.ID);

            modelBuilder.Entity<UserInfo>().ToTable("UserInfos");
            modelBuilder.Entity<UserInfo>().HasKey(i =>i.ID);

            ////针对“1:0或1:1”关系的设置
            modelBuilder.Entity<User>().HasOptional(u => u.UserInfo)
                .WithRequired(i => i.User);

            modelBuilder.Entity<Video>().ToTable("Videos");
            modelBuilder.Entity<Video>().HasKey(v => v.ID);

            modelBuilder.Entity<VideoRecord>().ToTable("VideoRecords");
            modelBuilder.Entity<VideoRecord>().HasKey(r => r.ID);

////针对“1:n”关系的设置
            modelBuilder.Entity<Video>().HasMany(v => v.Records)
            .WithRequired(r => r.Video);

测试程序

Context context = new Context();

            UserInfo userInfo = new UserInfo() { ID = 1, Name = "李占朋", Phone = "3205" };

            User user = new User() { ID = 1, UserName = "lizhanpeng", Passwd = "123456", UserInfo = userInfo };

            
            context.Set<User>().Add(user);
            context.SaveChanges();
            context.Set<User>().Include(x => x.UserInfo).
                ToList().ForEach(x =>
            {
                Console.WriteLine(x.UserName + "真实姓名为:" + x.UserInfo.Name);
            });
            
            
           
            List<VideoRecord> records = new List<VideoRecord>();
            records.Add(new VideoRecord() { ID = 1, IP = "127.0.0.1", CrateAt = DateTime.Now });
            records.Add(new VideoRecord() { ID = 2, IP = "127.0.0.1", CrateAt = DateTime.Now });
            Video video = new Video() { ID = 1, Title = "视频1", Records = records };
            context.Set<Video>().Add(video);
            context.SaveChanges();


            var vd = context.Set<Video>().Include(x => x.Records).FirstOrDefault();


            vd.Records.ToList().ForEach(r =>
            {
                Console.WriteLine("浏览者IP:" + r.IP + "浏览视频" + vd.Title);
            });

            //参考http://www.cnblogs.com/dudu/archive/2011/07/09/entity_framework_one_to_many.html
发表评论
用户名: 匿名