2017-07-29 4 views
0

私は、デバイスにデータを格納し、非常に時折それをサーバから取得する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; } 
} 

答えて

0

多くの関係に多くのためSQLite-Net extensionsをチェックしてください。ドキュメントから

引用:

外部キーがX-に-1 の関係を表すため、多対多の関係は、エンティティの 1の外部キーを使用して表現することはできません。代わりに、中間実体が必要です。この エンティティはアプリケーションで直接使用されることはありませんが、わかりやすくするために を振ると、SQLite-Net Extensionsはユーザ が明示的に定義していないテーブルを作成しません。

0

多対多関係を使用すると、中間エンティティが必要です。

は、このようなあなたのエンティティを定義してみてください。

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; } 

    [ManyToMany(typeof(DishIngredients))] 
    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; } 

    [ManyToMany(typeof(DishIngredients))] 
    public List<Dish> Dishes { get; set; } 
} 

public class DishIngredients 
{ 
    [ForeignKey(typeof(Dish))] 
    public int DishId { get; set; } 

    [ForeignKey(typeof(Ingredient))] 
    public int IngredientId { get; set; }  
} 
+0

変更した後、私はまだテーブルを作成することはできません。 https://ibb.co/gdboqQ – Perisher

+1

@Perisherエラーがこの問題に関連しているようです:https://github.com/praeclarum/sqlite-net/issues/553 – apineda

関連する問題