简体   繁体   English

Graphdiff正在删除实体

[英]Graphdiff is removing entities

I have the following entities: 我有以下实体:

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; }
}  

and

public class Functionality
{
    [Required]
    public string Name { get; set; }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
}  

In my service method, when I try the following, it removes from the database the Functionality objects that are not in Profile's Functionalities list: 在我的服务方法中,当我尝试以下操作时,它会从数据库中删除不在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();  
(...)  

Any ideas about it? 关于它的任何想法?
Thanks! 谢谢!

Found a solution after reading andypelzer's explanation about differences between Associated and Owned in this post: 在阅读了andypelzer关于这篇文章中关联和拥有之间差异的解释之后找到了解决方案:

https://github.com/refactorthis/GraphDiff/issues/43 https://github.com/refactorthis/GraphDiff/issues/43

So I changed 所以我改变了

((BaseRepository)this.Repository)
    .UpdateGraph(profile,map => map.OwnedCollection(p => p.Functionalities));
this.Repository.SaveChanges();  

to

((BaseRepository)this.Repository)
    .UpdateGraph(profile,map => map.AssociatedCollection(p => p.Functionalities));
this.Repository.SaveChanges();  

and works like a charm! 并且像一个魅力!

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

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