Reputation: 1088
I have an App in which I use SQLiteExtensions and following model:
[Table("SavedMeals")]
public class SavedMeal
{
[PrimaryKey, AutoIncrement] // Primary Key already indexed in Table
public int Id { get; set; }
[Unique] // Primary Key combined with ID
public string Name { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<FoodSelection> SelectedFoods { get; set; } = new List<FoodSelection>();
public DateTime DateLastSaved { get; set; } = DateTime.Now;
}
[Table("Foods")]
public class Food
{
[PrimaryKey, AutoIncrement] // Primary Key already indexed in Table
public int Id { get; set; }
[Unique] // Primary Key combined with ID
public string Name { get; set; }
public string Brand { get; set; }
}
[Table("FoodSelections")]
public class FoodSelection : Food
{
[PrimaryKey, AutoIncrement] // Primary Key already indexed in Table
public new int Id { get; set; }
[ForeignKey(typeof(SavedMeal))] // Specify the foreign key
public int SavedMealId { get; set; }
[ManyToOne(CascadeOperations = CascadeOperation.All)] // Many to one relationship with SavedMeal
public SavedMeal SavedMeal { get; set; }
public FoodSelection() : base()
{
}
public FoodSelection(Food foodItem) : base()
{
Id = foodItem.Id;
Name = foodItem.Name;
Brand = foodItem.Brand;
}
public bool IsSelected { get; set; } = false;
private int _min = 0;
public int Min
{
get => _min;
set
{
_min = value;
//NotifyPropertyChanged();
NotifyPropertyChanged(nameof(IsValid));
}
}
private int _max = 0;
public int Max
{
get => _max;
set
{
_max = value;
//NotifyPropertyChanged();
NotifyPropertyChanged(nameof(IsValid));
}
}
public bool IsValid { get => Max >= Min; }
}
In my DatabaseService I have an Init-Method and an UpdateMeal(...)-Method:
static async Task Init()
{
if (Database is not null)
{
return;
}
Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
await Database.CreateTableAsync<FoodSelection>();
await Database.CreateTableAsync<SavedMeal>();
await Database.CreateTableAsync<Food>();
}
public static async Task UpdateMeal(SavedMeal mealItem)
{
await Init();
await Database.UpdateWithChildrenAsync(mealItem);
}
A SavedMeal with a Name like "Lunch" consists of a List which is basically what the corresponding Lunch consists of (e.g. Chicken, Soup, Salad). Just as a background.
When I update a saved meal, I also want to update the food selection in the Database. I hope the relations and constraints are correct like this.
Anyway, when I do my UpdateWithAllChildrenAsync(...) with a meal my constraints are not updated and I can't see a change in table FoodSelections. I checked that the SavedMeal has the right Id (Primary Key) that is used for the Update and also the List has the new List I want to save.
I also tried with await Database.InsertOrReplaceWithChildrenAsync(mealItem, true);
also without success.
Is there anything wrong in my Code that prevents the tables from being updated correctly?
Upvotes: 0
Views: 169