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!
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.