EF-CodeFirst-表关系-延迟/贪婪加载

表关系建立

http://blog.csdn.net/niewq/article/details/17232321

一对多:

EF-CodeFirst-表关系-延迟/贪婪加载
namespace LckLib.EF.V6.Models
{
    public class Role
    {
        public int ID { get; set; }
        [MaxLength(20)]
        [Required]
        public string RoleName { get; set; }
        public string Remark { get; set; }
        public virtual List<User> Users { get; set; }
    }

    public class User
    {
        public int ID { get; set; }
        [MaxLength(20)]
        public string UserName { get; set; }
        public string Account { get; set; }
        public string Password { get; set; }
        public DateTime CreateDate { get; set; }
        public string Remark { get; set; }
        public virtual Role Role { get; set; }
    }
}
View Code

多对多:

EF-CodeFirst-表关系-延迟/贪婪加载
namespace LckLib.EF.V6.Models
{
    public class Activity
    {
        public int ActivityId { get; set; }
        [Required, MaxLength(50)]
        public string Name { get; set; }
        public List<Trip> Trips { get; set; }
    }

    public class Trip
    {
        public int TripId { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public decimal CostUSD { get; set; }
        public byte[] RowVersion { get; set; }
        public List<Activity> Activities { get; set; }
    }
}
View Code

一对一:

EF-CodeFirst-表关系-延迟/贪婪加载
namespace LckLib.EF.V6.Models
{
    public class Person
    {
        public int ID { get; set; }
        public int SocialSecurityNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [Timestamp]
        public byte[] RowVersion { get; set; }
        public PersonPhoto Photo { get; set; }
    }

    public class PersonPhoto
    {
        [Key, ForeignKey("PhotoOf")]
        public int ID { get; set; }
        public byte[] Photo { get; set; }
        public string Caption { get; set; }
        public Person PhotoOf { get; set; }
    }
}
View Code


延迟加载:

http://www.cnblogs.com/lei2007/p/3141051.html

第一:在需要延迟加载的属性前加上virtual ,该属性的类型可以是任务的集合类型ICOLLOCT<T>或者是0/1..1关联属性。

如: public virtual List<User> Users { get; set; }

第二:在context构造器中开启延迟加载功能,延迟加载默认是开启的。

EF-CodeFirst-表关系-延迟/贪婪加载
namespace LckLib.EF.V6
{
    public class LckDbContext : DbContext
    {
        public LckDbContext()
        {
            //默认是开启延迟加载
            this.Configuration.LazyLoadingEnabled = true;
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }

        public DbSet<Activity> Activities { get; set; }
        public DbSet<Trip> Trips { get; set; }

        public DbSet<Person> Persons { get; set; }
        public DbSet<PersonPhoto> PersonPhotos { get; set; }

    }
}
View Code

贪婪加载:

EF-CodeFirst-表关系-延迟/贪婪加载
            LckDbContext ef = new LckDbContext();
            var personInfo = from a in ef.Persons.Include("Photo")
                             select a;
            Console.WriteLine(personInfo.FirstOrDefault().Photo.Caption);
View Code

EF-CodeFirst-表关系-延迟/贪婪加载

上一篇:Http长连接200万尝试及调优


下一篇:大话西游2召唤兽数据预测工具1.7发布