Many levels of relationship

Issue #31 invalid
piotrek1231 created an issue

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)

  1. Guillermo Gutiérrez

    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.

  2. piotrek1231 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; }
    
  3. Guillermo Gutiérrez

    In that case you're only missing the recursive parameter of the insert method:

    _sqlLiteConnection.InsertWithChildren(timetable, recursive: true);
    
  4. piotrek1231 reporter

    Thanks for reply. Now, I have something like this:

    _sqlLiteConnection.InsertWithChildren(timetable, recursive: true);
    

    but Places are still null.

  5. Guillermo Gutiérrez

    You also need the recursive parameter when reading the objects from database...

    var results = _sqlLiteConnection.GetAllWithChildren<Timetable>(recursive: true);
    
  6. Log in to comment