简体   繁体   中英

Relational Databases: Database Structure / Flow

I'm using Entity Framework to setup a database. I'm brand new to relational databases and I'm attempting to determine the proper way to setup a few tables and their relations. Here's the scoop.

Say I have three tables in my database.

The main table is table A which holds a dataset for an object let's call this object Food . Columns: FoodID (primary key), RecipeID (foreign key paired to it's recipe in Table C).

Table C: contains records for recipes used to make the different Food items stored in Table A. Columns: RecipeID (primary key) and Recipe Name.

Table B: Is an instruction / recipe entry to for creating a Food . Columns: EntryID (primary key), RecipeID (foreign key referencing the Recipe ID in table C), FoodID (foreign key referencing a Food in Table A).

I can't wrap my ahead around the proper way to do this since it makes a circular relationship.

Do I just remove the foreign key ( RecipeID ) from the Food table? What is the proper flow I should be pursuing in situations like this.

 Recipes -> Multiple Recipe Entries -> Food -> Recipe

Food requires a recipe to make it but Food is used in recipes to make other Food .

Conceptualizing the data into C# code it would look like this.

public class Food
{
    public int FoodID { get; set; }
    public string Name { get; set; }
    public List<Food> Recipe { get; set; }
}

Entity Framework Model would be the following.

public class Food
{
    [Key]
    public int FoodID { get; set; }//Pri Key
    public string Name { get; set; }
    public int FoodRecipeID { get; set; }//Foreign Key

    public virtual FoodRecipe FoodRecipe { get; set; }//Navigation Property
}

public class FoodRecipeEntry
{
    [Key] 
    public int FoodRecipeEntryId { get; set; } //Pri Key
    public string Name { get; set; }
    public int FoodID { get; set; }//Foreign Key
    public int FoodRecipeID { get; set; }

    //Navigation Properties
    public Food Food { get; set; }
    public FoodRecipe FoodRecipe { get; set; }
}

public class FoodRecipe
{
    [Key]
    public int FoodRecipeID { get; set; } //Pri Key
    public string Name { get; set; }
    public virtual ICollection<FoodRecipeEntry> FoodRecipeEntries {get; set; }//Navigation Property
}

Here are some pointers:

  1. Remove the Food ID from FoodRecipeEntry , the FoodID is implied by the recipe since the recipe is for a specific food, and the entry belongs to the given recipe
  2. I'd personally put the foreign key inside Recipe, not Food - a single food may have multiple recipes, so it should be Recipe -> Food

In code this would look like so:

public class Food
{
    [Key]
    public int FoodID { get; set; }//Pri Key
    public string Name { get; set; }

    public virtual FoodRecipe FoodRecipe { get; set; }//Navigation Property
}

public class FoodRecipeEntry
{
    [Key] 
    public int FoodRecipeEntryId { get; set; } //Pri Key
    public string Name { get; set; }

    [ForeignKey("FoodRecipe")] // Personally I prefer using explicit foreign key attributes to avoid implicit jankiness
    public int FoodRecipeID { get; set; }

    [ForeignKey("Ingredient")]
    public int IngredientID { get; set; }

    //Navigation Properties
    public virtual FoodRecipe FoodRecipe { get; set; }


    public virtual Food Ingredient { get; set; }
}

public class FoodRecipe
{
    [Key]
    public int FoodRecipeID { get; set; } //Pri Key
    public string Name { get; set; }
    [ForeignKey("Food")] // Personally I prefer using explicit foreign key attributes to avoid implicit jankiness
    public int FoodID { get; set; }//Foreign Key
    public virtual Food Food { get; set; }
    public virtual ICollection<FoodRecipeEntry> FoodRecipeEntries {get; set; }//Navigation Property
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM