简体   繁体   中英

Entity Framework 6.0 updating List of items creates new records in the Database

The following are the entity classes to make more understanding of relationships:

public class EmployeeCv : UserEntity
    {
    public byte ProfileImage { get; set; }
    public virtual List<Header> Headers { get; set; }

    public virtual List<ProjectExperience> ProjectExperiences { get; set; }
    public virtual List<Tag> Tags { get; set; } //many to many relationship between employeeCv and tags

    [NotMapped]
    public List<TagsByTypes> TagsbyTypes
    {
        get
        {
            List<TagsByTypes> ListOfTagTypes = new List<TagsByTypes>();
            if (Tags != null)
            {
               var GroupedList = Tags.GroupBy(x => x.TagType.Title).ToList().Select(grp => grp.ToList());
               foreach (var currentItem in GroupedList)
               {
                    var TagType = new TagsByTypes()
                    {
                        Title = currentItem.FirstOrDefault().TagType.Title,
                        Tags = currentItem
                    };
                    ListOfTagTypes.Add(TagType);

               }
                return ListOfTagTypes;                         
            }
            else
                return null;
        }
    }
}

public class Tag : AuditableEntity<int>
    {
    public string Title { get; set; }
    public virtual List<EmployeeCv> EmployeeCv { get; set; }

    public virtual TagType TagType { get; set; }
    //To post Id's Not added to the database
    [NotMapped]
    public int TagTypeId { get; set; }
    [NotMapped]
    public int EmployeeCv_Id { get; set; }
}
public class TagType : AuditableEntity<int>
{
    public string Title { get; set; }
    public virtual List<Tag> Tags { get; set; }
}

I am writing a function to add new tag to the employeeCv based on the existing tag type. I have got Unit of work and Repositories setup to add/update/delete records in DB. Here is my implementation:

        public void UpdateEmployeeCVWithTag(Tag tag)
        {
            using (var repository = new UnitOfWork<EmployeeCv>().Repository)
            {
                var EmployeeCv = repository.GetSingleIncluding(tag.EmployeeCv_Id,
                   x => x.Headers, x => x.Tags,
                   x => x.ProjectExperiences,
                   x => x.ProjectExperiences.Select(p => p.AssociatedProject),
                   x => x.ProjectExperiences.Select(p => p.ProjectSkills));
                //x => x.ProjectExperiences.Select(p => p.ProjectSkillTags.Select(s => s.AssociatedSkill)));
                //tag.TagType = EmployeeCv;


                    var repositoryTagType = new UnitOfWork<TagType>().Repository;
                    var tagtype = repositoryTagType.GetItemById(tag.TagTypeId);
                    tag.TagType = tagtype; //even after assignment new tagtype is creating everytime code runs
                    //repositoryTag.UpdateItem(tagtype);
                    EmployeeCv.Tags.Add(tag);
                    //EmployeeCv.ProjectExperiences[projectId - 1].ProjectSkills.Add(tag);
                    repository.UpdateItem(EmployeeCv);

            }
        }

This function works correctly except one issue. It is creating a new TagType in the database and ignoring the one that already exist. Below is my updateItem code in the repository classs:

public virtual void UpdateItem(TEntity entityToUpdate)
        {
            var auditableEntity = entityToUpdate as IAuditableEntity;
            if (auditableEntity != null)
            {
                auditableEntity.UpdatedDate = DateTime.Now;
            }
            //_context
            //Attach(entityToUpdate);
            _context.Entry(entityToUpdate).State = EntityState.Modified;
            _context.SaveChanges();
        }

My guess without seeing the full functionality, is that you are using different context for this.

You should update the foreign key not the entire object so there is no need to add the entire TagType object since the tagTypeId is already set. The foreign key should work as is.

Please look into this link for further information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM