我有以下实体:
public class Profile
{
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public IList<Functionality> Functionalities { get; set; }
}
和
public class Functionality
{
[Required]
public string Name { get; set; }
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
}
在我的服务方法中,当我尝试以下操作时,它会从数据库中删除不在Profile的Functionalities列表中的Functionality对象:
(...)
var databaseFunctionalities = this
.Repository
.GetByIds<Functionality>(profile
.Functionalities
.ToIdList());
profile.Functionalities.Clear();
profile.Functionalities.AddRange(databaseFunctionalities);
((BaseRepository)this.Repository)
.UpdateGraph(profile,map => map.OwnedCollection(p => p.Functionalities));
this.Repository.SaveChanges();
(...)
关于它的任何想法?
谢谢!
答案 0 :(得分:3)
在阅读andypelzer对此帖中关联与拥有之间差异的解释后找到了解决方案:
https://github.com/refactorthis/GraphDiff/issues/43
所以我改变了
((BaseRepository)this.Repository)
.UpdateGraph(profile,map => map.OwnedCollection(p => p.Functionalities));
this.Repository.SaveChanges();
到
((BaseRepository)this.Repository)
.UpdateGraph(profile,map => map.AssociatedCollection(p => p.Functionalities));
this.Repository.SaveChanges();
并且像魅力一样!