简体   繁体   English

实体框架6.0更新项目列表在数据库中创建新记录

[英]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. 我正在写一个函数,根据现有标签类型将新标签添加到employeeCv中。 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. 它正在数据库中创建一个新的TagType,并忽略已经存在的TagType。 Below is my updateItem code in the repository classs: 下面是存储库类中的updateItem代码:

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. 您应该更新外键而不是整个对象,因此,由于已经设置了tagTypeId,因此无需添加整个TagType对象。 The foreign key should work as is. 外键应按原样工作。

Please look into this link for further information. 请查看此链接以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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