How to load the navigation property with EF Core?

前端 未结 3 705
再見小時候
再見小時候 2020-12-11 09:43

In EF6 we had such option:

context.Set().Attach(entity);
context.Entry(entity).Collection(\"NavigationProperty\").Load();

Si

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 09:53

    See below Code,
    I'm inserting data in UserRef table as well another table which we have many 2 many relationships.

    public void AddUser(User user, IEnumerable securityQuestion, string password)
        {
            var userModel = _mapper.Map(user);
            userModel.CreateTime = DateTime.Now;
    
            userModel.UserNewsLetterMaps.ToList().ForEach(u => this._context.UserNewsLetterMaps.Add(u));            
            this._context.RoleRefs.Attach(new RoleRef() { RoleId = (int)user.UserRole, UserRefs = new List { userModel } });
            userModel.ResidenceStatusRefs.ToList().ForEach(u => this._context.ResidenceStatusRefs.Attach(u));
            this._context.UserRefs.Add(userModel);
            this.Save();
        }
    

提交回复
热议问题