大佬教程收集整理的这篇文章主要介绍了c# – Entity Framework 6,急切加载嵌套关系返回空集合,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public class User { [Key] [DatabaseGenerated(DatabaseGeneratedoption.Identity)] public virtual int UserId { get; set; } private ICollection<Recipe> _recipes; public virtual ICollection<Recipe> Recipes { get { return _recipes ?? new List<Recipe>(); } set { _recipes = value; } } } public class Recipe { [Key] [DatabaseGenerated(DatabaseGeneratedoption.Identity)] public int RecipEID { get; set; } private ICollection<Ingredient> _ingredients; public virtual ICollection<Ingredient> Ingredients { get { return _ingredients ?? new List<Ingredient>(); } set { _ingredients = value; } } public User User { get; set; } } public class Ingredient { [Key] [DatabaseGenerated(DatabaseGeneratedoption.Identity)] public int IngredientId { get; set; } public Recipe Recipe { get; set; } }
简而言之,用户与Recipe有一对多的关系,而Recipe与Ingredient之间存在一对多的关系.我正在使用代码第一种方法,在受保护的覆盖中使用此映射void OnModelCreaTing(DbModelBuilder modelBuilder):
@H_878_7@modelBuilder.Entity<User>() .Hasmany(u => u.Recipes) .Withrequired(u => u.User) .WillCascadeOndelete(true); modelBuilder.Entity<Recipe>() .Hasmany(u => u.Ingredients) .Withrequired(u => u.RecipE) .WillCascadeOndelete(true);
我正在使用存储库,它具有此代码将数据存储到MysqL数据库中:
public void Add(User entity) { using (var co@R_944_10443@t = new UserCo@R_944_10443@t()) { co@R_944_10443@t.Users.Add(entity); co@R_944_10443@t.SaveChanges(); } }
和取:
public User GetById(int id) { using (var co@R_944_10443@t = new UserCo@R_944_10443@t()) { var user = co@R_944_10443@t.Users .Include(u => u.Recipes) .Include(u => u.Recipes.SELEct(x => x.Ingredients)) .FirstOrDefault(u => u.UserId == id); return user; } }
我有集成测试,它创建一个User对象,List< Recipe>具有两个配方,其中每个配方具有List< Ingredient>有2件物品.每种食谱都有所不同,总共有4种成分.当我将它添加到存储库时,我可以在数据库中看到它具有正确的PK和FK.
但是,从数据库中检索时,返回的User对象具有配方,但这些配方都没有配方.由于我设置我的getter的方式,当我尝试访问它们时,我收到一个空集合.
例如这样做:
/* create recipes collection and seed it */ User user = new User {Recipes = recipes /* plus some omitted properites*/}; _repository.Add(user); var returnedUser = _repository.GetById(user.UserId); var userIngredients = user.Recipes.First(u => u.Recipename == "Test").Ingredients.ToList(); var returnedIngredients = returnedUser.Recipes.First(u => u.Recipename == "Test").Ingredients.ToList();
returnedIngredients为空,而userIngredients有两个元素.然后这会失败断言并导致集成测试失败.
有人能告诉我如何在嵌套的一对多上正确地进行加载吗?
get { return _recipes ?? new List<Recipe>(); }
应该读:
get { return _recipes ?? (_recipes = new List<Recipe>()); }
成分相同.
否则,每次都会返回一个新的空列表(因为它没有存储在任何地方).这仅在整个列表设置在某一点(从数据库读取并覆盖)时才有效,但如果您是按代码创建实体则不会,因为它们将被添加到未存储在支持字段中的列表中.
你看到Recipes已满,因为你在这里设置它:User user = new User {Recipes = recipes / *加上一些省略的属性* /};所以它存储在支持字段中.
但是当你添加它们时,成分不是:
var recipe = new Recipe(); recipe.Ingredients.Add(myIngredient); // <-- this will be stored in a // new List<Ingredient>,// but not on the // _ingredients backing field // since the get accesor doesn't // set it recipe.Ingredients.Add(myIngredient); // <-- this will be stored in a // new List<Ingredient>,not in // the expected created one
至于急切的加载,你不需要这个:
var user = co@R_944_10443@t.Users .Include(u => u.Recipes) .Include(u => u.Recipes.SELEct(x => x.Ingredients)) .FirstOrDefault(u => u.UserId == id);
只需:
var user = co@R_944_10443@t.Users .Include(u => u.Recipes.SELEct(x => x.Ingredients)) .FirstOrDefault(u => u.UserId == id);
它会加载两个级别
以上是大佬教程为你收集整理的c# – Entity Framework 6,急切加载嵌套关系返回空集合全部内容,希望文章能够帮你解决c# – Entity Framework 6,急切加载嵌套关系返回空集合所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。