私は、デバイスにデータを格納し、非常に時折それをサーバから取得するXamarin.Androidにデータベースを実装することにしました。とにかく、テーブルを格納していない場合は、ゼロからデータベースを作成したかったのです。2番目のテーブルがまだ作成されていないため、コンストラクタクラッシュで多対多テーブルを作成します
私の問題は、新しいテーブルを作成するためのモデルがありますが、それらのモデルは多対多の関係で接続されており、最初にテーブルを作成しようとするとトランザクションがクラッシュすることです。
私はそれが制約に関連していることを知っていますが、私はこれを避けるためにアプリをリファクタリングする方法はわかりません。私はこれに直接関係する返事を見つけることができませんでした。
どのような考えですか?
class Database
{
private SQLiteConnection _connection;
public Database()
{
_connection = new SQLite().GetConnection();
LogHelper.CustomLog("SQL Created");
_connection.BeginTransaction();
_connection.CreateTable<Dish>(); //there is the issue
_connection.CreateTable<Ingredient>();
_connection.Commit();
LogHelper.CustomLog("SQL DBs created");
AddIngredient(new Ingredient() { Id = 1, Name = "apple", Vegetarian = false, GlutenFree = false });
AddDish(new Dish() { Id = 1, Calories = "23", Desc = "test", Name = "oranges", Time = 30, Ingredients = GetIngredientList() });
}
}
public class Dish
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Desc { get; set; }
public int Time { get; set; }
//public string Rating { get; set; }
public string Calories { get; set; }
[ForeignKey(typeof(Ingredient))]
public int IngredientId { get; set; }
[ManyToMany(typeof(Dish))]
public List<Ingredient> Ingredients { get; set; }
public override string ToString()
{
return string.Format("[Dish: Id={0}, Name={1}, Desc={2}, Time={3},
Ingredients={4}, Calories={6}]",
Id, Name, Desc, Time, Ingredients, Calories);
}
}
public class Ingredient
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public bool Vegetarian { get; set; }
public bool GlutenFree { get; set; }
[ForeignKey(typeof(Dish))]
public int DishId { get; set; }
[ManyToMany(typeof(Ingredient))]
public List<Dish> Dishes { get; set; }
}
変更した後、私はまだテーブルを作成することはできません。 https://ibb.co/gdboqQ – Perisher
@Perisherエラーがこの問題に関連しているようです:https://github.com/praeclarum/sqlite-net/issues/553 – apineda