Graphdiff is removing entities

entity-framework graphdiff

Question

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:

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

1
1
10/9/2014 8:55:12 AM

Accepted Answer

Found a solution after reading andypelzer's explanation about differences between Associated and Owned in this post:

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!

3
10/9/2014 12:31:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow