使用条目< TEntity>()。CurrentValues.SetValues()不更新集合 [英] Using the Entry<TEntity>().CurrentValues.SetValues() is not updating collections

查看:455
本文介绍了使用条目< TEntity>()。CurrentValues.SetValues()不更新集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前没有遇到过,因为我通常自己处理集合,而不是直接在实体上修改它们。

  public class Schedule:BaseEntity 
{
public Guid Id {get; set;}
public virtual int? DayOfTheWeekTypeId {get;组; }
public virtual DayOfTheWeekType DayOfTheWeekType {get;组; }
public virtual ICollection< Instructor>导师{get;组; }
public DateTime? StartDateTime {get;组; }
public DateTime? EndDateTime {get;组; }
public string SpecialInstructions {get;组; }
}

映射类:

  public Sc​​heduleMapping()
{
HasMany(c => c.Instructors).WithMany()。Map(m => {m.MapLeftKey (ScheduleId);
m.MapRightKey(InstructorId);
m.ToTable(Schedule_Instructors);
});
HasOptional(s => s.DayOfTheWeekType).WithMany()。HasForeignKey(s => s.DayOfTheWeekTypeId).WillCascadeOnDelete(false);
属性(s => s.SpecialInstructions).IsMaxLength();
}

这是我的更新方法:

  public virtual void Update(TEntity entity)
{
if(entity == null)
throw new ArgumentNullException(entity) ;
//这是原始持久化实体
var persistedEntity = _repository.GetById(entity.Id);
if(originalEntity!= null)
{
entity.Id = persistedEntity.Id;
UnitOfWork.ApplyCurrentValues< TEntity>(originalEntity,entity);
UnitOfWork.Commit();
}
}

这是处理合并 / p>

  public void ApplyCurrentValues< TEntity>(TEntity original,TEntity current)其中TEntity:class 
{
base .Entry< TEntity>(原始).CurrentValues.SetValues(电流);
}

如果我修改了教师集合,然后尝试应用更新,保持我的原始价值。我已经尝试在更新之前加载Schedule实体,并进行更改,但有时会在实体框架中导致PK错误(在Instructors集合中)。好像它试图添加一个具有相同密钥的实体。所以,我手动重建计划实体(包括ID),然后更新它。当我这样做,我不会再犯错误了,但是教练的收藏没有改变。我在想,因为CurrentValues。基于持久化实体应用SetValues,而不是我的更新版本。我应该以不同的方式处理我的更新,或者我需要manully

解决方案

SetValues 从不更新导航属性。执行代码时,只知道传递给您的更新方法的实体的简单/复杂属性的更改。 EF甚至不知道实体的相关实体传递给您的更新方法。



您必须手动告诉EF关于对象图中的每个变化 - EF没有任何对象图的分辨率机制


I have not run into this before, because I usually handled collections by them selves instead of modifying them directly on the entity.

public class Schedule: BaseEntity
        {
            public Guid Id {get;set;}
            public virtual int? DayOfTheWeekTypeId { get; set; }
            public virtual DayOfTheWeekType DayOfTheWeekType { get; set; }
            public virtual ICollection<Instructor> Instructors { get; set; }
            public DateTime? StartDateTime { get; set; }
            public DateTime? EndDateTime { get; set; }
            public string SpecialInstructions { get; set; }
        }

Mapping class:

    public ScheduleMapping()
            {
                HasMany(c => c.Instructors).WithMany().Map(m => { m.MapLeftKey("ScheduleId");
m.MapRightKey("InstructorId"); 
m.ToTable("Schedule_Instructors");
                });
                HasOptional(s => s.DayOfTheWeekType).WithMany().HasForeignKey(s => s.DayOfTheWeekTypeId).WillCascadeOnDelete(false);
                Property(s => s.SpecialInstructions).IsMaxLength();
            }

This is my update method:

public virtual void Update(TEntity entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
            //this is the original persisted entity
            var persistedEntity = _repository.GetById(entity.Id);
            if(originalEntity != null)
            {
                entity.Id = persistedEntity.Id;                
                UnitOfWork.ApplyCurrentValues<TEntity>(originalEntity,entity);
                UnitOfWork.Commit();
            }
        }

This is the Method that handled the "merge"

public void ApplyCurrentValues<TEntity>(TEntity original, TEntity current) where TEntity : class
        {
            base.Entry<TEntity>(original).CurrentValues.SetValues(current);
        }

If I modify the Instructors collection then try to apply the update, it seems to keep my original values. I have tried loading the the Schedule entity prior to the update and make my changes, but sometimes that causes a PK error (on the Instructors collection) in entity framework. As if it is trying to add an entity with the same key. So, instead I am rebuilding the Schedule entity (including the ID) manually and then updating it. When I do that I do not get any more errors, however, the Instructors collections doesn't change. I am thinking because CurrentValues. SetValues is being applied based on the persisted entity and not my updated version. the Should I handle my updates differently or do I need to manully

解决方案

SetValues never updates navigation properties. When you execute your code it only knows about changes in simple / complex properties of the entity passed to your Update method. EF even don't know about related entities of the entity passed to your Update method.

You must manually tell EF about each change in your object graph - EF doesn't have any resolution mechanism for object graphs.

这篇关于使用条目&lt; TEntity&gt;()。CurrentValues.SetValues()不更新集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆