entity framework update many to many relationship: virtual or not

后端 未结 3 772
孤独总比滥情好
孤独总比滥情好 2020-12-01 02:58

I\'ve been using EF4 (not code-first) since a year, so I\'m not really an expert with it. I\'ve a doubt in using many-to-many relationship regarding save n update.

3条回答
  •  醉梦人生
    2020-12-01 03:49

    I am using db-first approach and automapper to map between model and entity (MVC 5) and also using eager loading.

    In my scenario, there are equipments and there can be multiple users as equipment operators:

        public void Create()
        {
            using (var context = new INOBASEEntities())
            {
                // first i need to map model 'came from the view' to entity 
               var _ent = (Equipment)Mapper.Map(this, typeof(EquipmentModel), typeof(Equipment));
    
                context.Entry(_ent).State = System.Data.Entity.EntityState.Added;
    
    
                // I use multiselect list on the view for operators, so i have just ids of users, i get the operator entity from user table and add them to equipment entity
                foreach(var id in OperatorIds)
                {
                    AspNetUsersExtended _usr = context.AspNetUsersExtended.Where(u => u.Id == id).FirstOrDefault();
                    // this is operator collection
                    _ent.AspNetUsersExtended2.Add(_usr);
                }
    
                context.SaveChanges();
                Id = _ent.Id;
            }
        }
    
    
        public void Update()
        {
            using (var context = new INOBASEEntities())
            {
                var _ent = (Equipment)Mapper.Map(this, typeof(EquipmentModel), typeof(Equipment));
    
                context.Entry(_ent).State = System.Data.Entity.EntityState.Modified;
    
    
                var parent = context.Equipment
                            .Include(x => x.AspNetUsersExtended2)//include operators
                            .Where(x => x.Id == Id).FirstOrDefault();
                parent.AspNetUsersExtended2.Clear(); // get the parent and clear child collection
    
                foreach (var id in OperatorIds)
                {
                    AspNetUsersExtended _usr = context.AspNetUsersExtended.Where(u => u.Id == id).FirstOrDefault();
                    parent.AspNetUsersExtended2.Add(_usr);
                }
    
                // this line add operator list to parent entity, and also update equipment entity 
                context.SaveChanges();
            }
        }
    

提交回复
热议问题