2017-01-17 47 views
0

我正在創建一個Asp.net MVC應用程序,其中我有Recipe ModelRecipeCategory模型。我想食譜模型包含foreign keyRecipeCategory模型。我是Entity Framework的新手。我正在使用實體框架的code first方法。 我已經定義recipeCategory模型如下:在ASP.net中創建外鍵關係MVC實體框架代碼首先

public class RecipeCategory 
{ 
    public int RecipeCategoryID { get; set; } 
    public string Name { get; set; } 
    public List<Recipe> Recipe { get; set; } 
} 

和我有類似下面的食譜模型類:

public class Recipe 
{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 

    public int RecipeCategoryID { get; set; } 
    public RecipeCategory RecipeCategory { get; set; } 
} 

我想要什麼:

  1. 在創建新的配方,我想用戶界面中的類別列表來選擇RecipeCategories,但我無法得到。

  2. 我想在兩個表之間建立正確的外鍵。請幫我解決這個

回答

1

你的關係是正確的。爲了獲得更多改進,我建議您的ICollection使用複數名稱,而不是Recipe,而用ICollection替換List。

public class RecipeCategory 
{ 
    public int RecipeCategoryID { get; set; } 
    public string Name { get; set; } 
    public ICollection<Recipe> Recipes { get; set; } 
} 

public class Recipe 
{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 

    public int RecipeCategoryID { get; set; } 
    public RecipeCategory RecipeCategory { get; set; } 
} 

現在您已經說過您無法獲得食譜分類,所以您可以發佈您正在使用的代碼嗎?

它應該是這樣的: 1.您必須創建了一個從DbContext繼承的上下文類,並且您必須已經在DbSets中定義了這兩個實體。

public class YourContext: DbContext 
{ 
    public YourContext(): 
     base("yourConnectionString") 
    { 
    } 

    public DbSet<Recipe> Recipes { get; set; } 
    public DbSet<RecipeCategory> RecipeCategories { get; set; } 
} 

之後,你可以回到你的類別是這樣的:

public IEnumerable<RecipeCategory> Get() 
{ 
    YourContext context = new YourContext(); 
    return context.RecipeCategories; 
} 
+0

我創建視圖配方模型,並在創建視圖,我沒有從列表中選擇類別選項?如果我的關係正確,我該怎麼辦? –

+0

我必須得到單獨的食譜分類?我從強類型模型食譜創建視圖?它不應該存在於創作視圖中嗎? –

+0

好的。因此,在您的創建操作方法中,您需要從數據庫獲取RecipeCategories的列表,然後將其存儲在ViewBag中。然後在您的視圖中,您可以渲染viewbag的下拉菜單。請參考http://stackoverflow.com/questions/16594958/how-to-use-a-viewbag-to-create-a-dropdownlist和http://www.compilemode.com/2016/01/bind- dropdownlist-using-viewbag-in-asp-net-mvc.html @DelicateHiba –

0
public class RecipeCategory 
{ 
[Key][DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 
    public int RecipeCategoryID { get; set; } 
    public string Name { get; set; } 
    public List<Recipe> Recipe { get; set; } 
} 


public class Recipe 
{ 

    public int ID { get; set; } 

    public virtual int RecipeCategoryID { get; set; } 

    [ForeignKey("RecipeCategoryID ")] 
    public virtual ApplicationUser ApplicationUser { get; set; } 

    public string Title { get; set; } 
    public string Description { get; set; } 

    public int RecipeCategoryID { get; set; } 
    public RecipeCategory RecipeCategory { get; set; } 
} 
1

你需要把外鍵約束表。

[ForeignKey("ObjName")] 

在你的情況,

public class Recipe 
{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    [ForeignKey("RecipeCategory")] 
    public int RecipeCategoryID { get; set; } 
    public RecipeCategory RecipeCategory { get; set; } 
} 
相關問題