1

I am working on a .Net MAUI app that uses sqlite-net-pcl and sqlite-net-extensions. My goal is to make a RecipeBook like app. For that I need to have a database, here is the concept so far, that I want to make: Hope you can read everything.

So far I have made these Classes:

using SQLite;
using SQLiteNetExtensions.Attributes;

namespace KitchenRecipes.MyClasses;

public class RecipePage
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }

    public string Title { get; set; }

    public string HowToMake { get; set; }

    //public string Ingredients { get; set; }

    [ForeignKey(typeof(Ingredients))]
    public List<Ingredients> IdIngredients { get; set; }

    public RecipePage() { }

    public RecipePage(string title, string howToMake, string ingredients)
    {
        Title = title;
        HowToMake = howToMake;
        //Ingredients = ingredients;
        IdIngredients = new List<Ingredients>();//Egyenlore
    }

    public RecipePage(RecipePage rp)
    {
        id = rp.id;
        Title = rp.Title;
        HowToMake = rp.HowToMake;
        //Ingredients = rp.Ingredients;
        IdIngredients = new List<Ingredients>();//Egyenlore
    }
}
using SQLite;
using SQLiteNetExtensions.Attributes;

namespace KitchenRecipes.MyClasses;
public class Product
{
    [PrimaryKey, AutoIncrement]
    public int id {  get; set; }

    [ForeignKey(typeof(Ingredients))]
    public int idIngredient { get; set; }
    
    public string name { get; set; }

    public string unit { get; set; }

    public Product(){}

    public Product(Product p)
    {
        id = p.id;
        name = p.name;
        unit = p.unit;
    }

    public Product(int id, string name, string unit)
    {
        this.id = id;
        this.name = name;
        this.unit = unit;
    }
}
using SQLite;
using SQLiteNetExtensions.Attributes;

namespace KitchenRecipes.MyClasses;
public class Ingredients
{
    [PrimaryKey, AutoIncrement]
    public int id { get; set; }

    public string quantity { get; set; }

    [ManyToOne]
    public int idRecipe { get; set;}

    [ManyToOne]
    public List<Product> idProduct { get; set;}
}

And to really make and manage the whole database:

using System.Diagnostics;
using KitchenRecipes.MyClasses;
using SQLite;

namespace KitchenRecipes.Services;

public static class RecipePageService
{
    private static SQLiteAsyncConnection db = null;

    static async Task Init()
    {
        if (db != null)
        {
            return;
        }

        var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyData.db");

        try
        {
            db = new SQLiteAsyncConnection(databasePath); // Get an absolute path to the database file
            await db.CreateTableAsync<Product>();
            await db.CreateTableAsync<RecipePage>();
            await db.CreateTableAsync<Ingredients>();
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex + "\n\n");
        }
    }

    public static async Task RemoveRecipePage(int id)
    {
        //Doesnt matter
    }

    public static async Task PurgeRecipePage()
    {
        //Doesnt matter
    }

    public static async Task<IEnumerable<RecipePage>> GetRecipePage()
    {
        //Doesnt matter
    }

    public static async Task<RecipePage> AddRecipePage(RecipePage recipePage)
    {
        await Init();

        List<RecipePage> lista = await GetRecipePage() as List<RecipePage>;
        for (int i = 0; i < lista.Count; i++)
        {
            if (lista[i].Title == recipePage.Title)
            {
                if (lista[i].HowToMake == recipePage.HowToMake)
                {
                    return null;
                }
            }
        }

        Ingredients ing = new Ingredients();//TESZT
        Product pr = new Product();//TESZT

        await db.InsertAsync(pr);
        await db.InsertAsync(ing);
        await db.InsertAsync(recipePage);//HERE I GET AN EXCEPTION THAT SAYS: SQLite.SQLiteException: table RecipePage has no column named IdIngredients

        //Kikeresi azt amit berakott
        var q = db.Table<RecipePage>();
        q = q.Where(x => x.Title.Equals(recipePage.Title));
        q = q.Where(x => x.HowToMake.Equals(recipePage.HowToMake));
        var myBook = q.FirstOrDefaultAsync();

        var returnable = myBook.Result;
        return returnable;
    }
}

My question is what am I doing wrong? It worked when I only had 1 table, the RecipePage table, but now I want to be able to have ingredients not just a huge plain text that says how I have to make that reciepe.

I tried to specifially say that it has that column, by giveing the `[Column("...")]` tag to the ForeignKey. And got the same exception.

Albina
  • 1,901
  • 3
  • 7
  • 19
  • 1
    which line causes the exception? And please post the complete exception message in the body of the post, not just the title – Jason Mar 23 '23 at 18:00
  • There are several errors. First you're using a `ForeignKey` on a `List` property and later you use a `ManyToOne` attribute on a `int` property. Neither of those make sense. – redent84 Mar 23 '23 at 23:15
  • That was the problem thanks! I used ForeignKey on a List! – Bence Hérincs Mar 25 '23 at 08:46

0 Answers0