0

I made 2 tables in an SQLite database - Ingredients and Recipes.

A Recipe object uses Ingredient objects, naturally.

2 things I am trying to do: Get an ingredient object out of my database of ingredients and set that ingredient object as a variable when creating a new recipe.

//This ingredient data was just for testing purposes - this still wouldn't work. This section is just trying to set an ingredient object as the Ingredient1 variable in the Recipe object.

            InventoryItem theIngredient= new InventoryItem();

            theIngredient.ID = 500;
            theIngredient.Name = "Name";
            theIngredient.Size = "None";
            theIngredient.Stock = "None";


            Recipe recipe = new Recipe()
            {
                ID = (maxPK == null ? 1 : maxPK.ID + 1),
                Name = nameEntry.Text,
                Ingredient1 = theIngredient
            };
        db.Insert(recipe);

This is the error I'm getting:

System.NotSupportedException Message=Don't know about PantryShopper.Models.InventoryItem

This isn't even exactly what I want to do...

What I want to do is to select the ingredient from a picker that is sourced by the database, then use the selected ingredient from the picker as the ingredient passed into the new recipe. However, on THAT I can't seem to access that item from the database AS an ingredient object. I was thinking in the way I convert that object into data for a database, can I get it back from the database as the original object version, or do I have to rebuild it (which is what I was trying to do as well)?

        InventoryItem theIngredient= new InventoryItem();

        ingredient1Picker = new Picker();
        ingredient1Picker.ItemsSource = db.Table<InventoryItem>().OrderBy(x => x.Name).ToList();
        stackLayout.Children.Add(ingredient1Picker);

        inventoryItem = ingredient1Picker.SelectedItem;

Then set the ingredient to the currently selected item in the picker. However, I get the error: Cannot implicitly convert type 'object' to 'PantryShop.Models.InventoryItem,' An Explicit conversion exists (are you missing a cast?)

Anyway, this is my first post here. Any help is much appreciated! Cheers.

Cole
  • 5
  • 3
  • It sounds like you could benefit from an ORM. [Dapper](https://dapper-tutorial.net/) is what I use for C# projects.. This would simplify the process of mapping your db tables to C# classes. Reference [this question](https://stackoverflow.com/questions/6995291/how-can-i-use-dapper-to-connect-to-a-sqlite-database) – dpberry178 Apr 13 '20 at 18:58
  • where is the definition of InventoryItem? Is Ingredient the same as InventoryItem, or does it have it's own class? Your question is all over the place - it would help a lot if you focused on one specific issue and posted the relevant code for that issue. – Jason Apr 13 '20 at 19:12
  • @Jason Yeah, making those more similar probably would have helped in clarity. Yes, Ingredient is the same as InventoryItem. – Cole Apr 13 '20 at 19:23
  • I think the primary issue I'm having is this error: "System.NotSupportedException Message=Don't know about PantryShopper.Models.InventoryItem" Which seems to be related to just trying to pass an InventoryItem (theIngredient) to a new recipe object. I think the second part of the whole issue will be more clear if I understand the first part... I just thought to post the entire issue would have been better, probably not lol. – Cole Apr 13 '20 at 19:33
  • 1
    @dpberry178 I'm also looking into Dapper now, so thanks for that too! – Cole Apr 13 '20 at 20:07

1 Answers1

0

However, I get the error: Cannot implicitly convert type 'object' to 'PantryShop.Models.InventoryItem,' An Explicit conversion exists (are you missing a cast?)

as the error tells you, you need to explicitly cast SelectedItem (an object) to InventoryItem

inventoryItem = (InventoryItem)ingredient1Picker.SelectedItem;
Jason
  • 86,222
  • 15
  • 131
  • 146
  • I see, that makes sense for the second part. I didn't realize that was how to cast it, "explicitly" with the...cast in front like that. lol – Cole Apr 13 '20 at 19:36
  • for your other issue, see https://stackoverflow.com/questions/32153336/how-to-specify-a-foreign-key-property-in-sqlite-net-extensions – Jason Apr 13 '20 at 19:37