Many levels of relationship
Hi, I have table Timetable which contains list of Activity (One To Many). Activity contains list of Place (One To Many).
Below sample code which adding data:
Timetable plan = new Timetable();
plan.Id = "idKey";
plan.Version = "1.0";
plan.Activities = new List<Activity>();
Activity act1 = new Activity() { Name = "Activity name 1", Id = 200, Places = new List<Place>() };
act1.Places.Add(new Place { Id = 444, Name = "Place - activity 1" });
Activity act2 = new Activity() { Name = "Activity name 2", Id = 201, Places = new List<Place>() };
act2.Places.Add(new Place { Id = 443, Name = "Place - activity 2" });
plan.Activities.Add(act1);
plan.Activities.Add(act2);
Next, I add this to db and read:
_sqlLiteConnection.InsertWithChildren(timetable);
var results = _sqlLiteConnection.GetAllWithChildren<Timetable>();
In results I have Timetable object witch 2 Activity, but in Activity list of Place is still null.
Thanks
Comments (6)
-
-
- changed status to invalid
Closed because it's not an issue. Please consider using StackOverflow with the
sqlite-net-extensions
tag to ask questions and this tool to report issues. -
reporter I think that I did it in right way:
Piece of Timetable:
private List<Activity> _activities; [JsonProperty(PropertyName = "activities")] [Indexed] [OneToMany(CascadeOperations = CascadeOperation.All)] public List<Activity> Activities { get { return _activities; } set { _activities = value; RaisePropertyChanged("Activities"); } }
Piece of Activity:
[ForeignKey(typeof(Timetable))] public string TimetableId { get; set; } [ManyToOne] public Timetable Timetable { get; set; } private List<Place> _places; [JsonProperty(PropertyName = "category")] [OneToMany(CascadeOperations = CascadeOperation.All)] public List<Place> Places { get { return _places; } set { _places = value; RaisePropertyChanged("Places"); } }
Piece of Place:
[ForeignKey(typeof(Activity))] public string ActivityId { get; set; } [ManyToOne] public Activity Activity { get; set; }
-
In that case you're only missing the
recursive
parameter of the insert method:_sqlLiteConnection.InsertWithChildren(timetable, recursive: true);
-
reporter Thanks for reply. Now, I have something like this:
_sqlLiteConnection.InsertWithChildren(timetable, recursive: true);
but Places are still null.
-
You also need the
recursive
parameter when reading the objects from database...var results = _sqlLiteConnection.GetAllWithChildren<Timetable>(recursive: true);
- Log in to comment
You are not inserting the activities into the database, and therefore they are not being assigned when fetching the timetable. Take a look at the Cascade operations section of the documentation where it's explained how to insert a full tree of objects in the database, and then read to memory again.