关于Entity Framework自动关联查询与自动关联更新导航属性对应的实体注意事项说明
发布日期:2021-05-09 09:35:00 浏览次数:17 分类:博客文章

本文共 5774 字,大约阅读时间需要 19 分钟。

���������������������Entity Framework ���������������������

Entity Framework ���������������������������������������Lazy Loading������������������,Eager Loading������������������,Explicit Loading���������������������������Lazy Loading���Explicit Loading���������������������

(������������Entity Framework������������������������������������������������DB First,Model First,Code First���������������Entity������������������������������������������������������������������������������������������������Entity Framework 6.0,���������Code First������������������Entity���������)

������Lazy Loading���������������������������������������(���������������������������������������Configuration.LazyLoadingEnabled=true,������������������false���������)���������������������������������������������POCO������������������������������������������������������������������������������������������������������

1.���������������������������public������������Sealed���

2.���������������Virtual

��������������������������������������������������������������������������������������������������� ���������������������������������������������������������������������������������������������������������������������

���������������������

[Table("User",Schema="dbo")]    public class User    {        [Key]        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]        [Display(Name = "������ID")]        public int ID { get; set; }        [Required]        [MinLength(6)]        [MaxLength(15)]        [Unique("User", "UserName")]        [Display(Name = "���������")]        public string UserName { get; set; }        [Required]        [Display(Name = "������")]        public string Password { get; set; }        [Required]        [Display(Name = "������������")]        public int UserTypeID { get; set; }        [ForeignKey("UserTypeID")]        public virtual UserType UserType { get; set; } //������������������User���������������������UserTypeID ������������UserType ������������������������UserType ������    }

������Eager Loading������������������������LazyLoadingEnabled������false���������������������������Virtual���������������������IQueryable���������������Include������������������������������������

���������Include������������������������������������������������������������������������������������������������������������������������Include���������������������������������������������������������������������������������������������������������������������������������

���������������������

var entitiesContext=new TEMSContext();entitiesContext.Configuration.LazyLoadingEnabled=false;var users = entitiesContext.Users.Include("UserType");//var users = entitiesContext.Set
().Include("UserType");���������������������Assert.AreNotEqual(null, users.First().UserType);

������Explicit Loading������������������������LazyLoadingEnabled������false���������������������������Virtual���������������������DbEntityEntry.Reference������������������������������������������������������DbEntityEntry.Collection���������������������������������������������������������������DB First���������������ObjectContext.LoadProperty������������������������������������������

���������������������������������������������������������������������������������������Reference���Collection���������������������������������������Load������������������ObjectContext.LoadProperty������������������������������������������������������������������,���������������������������������������������������������������������������

���������������������

//������Code First���������������������������var entitiesContext = new TEMSContext();            entitiesContext.Configuration.LazyLoadingEnabled = false;            var user = entitiesContext.Users.First();            var u = entitiesContext.Entry(user);            u.Reference(t => t.UserType).Load();            Assert.AreNotEqual(null, user.UserType);//������DB First������������������������������var db = new aTestEntities();            db.ContextOptions.LazyLoadingEnabled = false;            var comp = db.Companies.First();            db.LoadProperty(comp, t => t.Departments);            Assert.AreNotEqual(0, comp.Departments.Count);

���������������������������������������������������������������������

������������Lazy Loading���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

var entitiesContext = new TEMSContext();User user = new User() {UserName="testuser",RealName="������������",CompanyID = 1, Company = UserBusiness.CurrentUser.Company };entitiesContext.Users.Add(user);entitiesContext.SaveChanges();

 ������������������CompanyID���������������Company���������������������������������������������������������������Company���ID������1(Company������������ID���1���������),UserBusiness.CurrentUser���������������������������������������������������������������������������������User������������������������������������������������Company������������������������(������������������������������ID=1)���������������������ID���������CompanyID������������2������������������User������������������������,������������User.CompanyID=2������������1������������������������������������������������������������������������������������������������������������������������CompanyID,���������������Company������������������������������������������������������������������������������������������������������������

 

���������zuowj.cnblogs.com

上一篇:实现winform DataGridView控件判断滚动条是否滚动到当前已加载的数据行底部
下一篇:阅读《LEARNING HARD C#学习笔记》知识点总结与摘要系列文章索引

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月05日 17时13分43秒