Graphdiff正在删除实体

时间:2014-10-08 22:01:22

标签: entity-framework graphdiff

我有以下实体:

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();  
(...)  

关于它的任何想法?
谢谢!

1 个答案:

答案 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();  

并且像魅力一样!