使用Entity Framework中的导航属性填充多对多关系表

我正在学习asp.net mvc应用程序中的Entity Framework。 我有3个型号 –

AppModel,CategoryModel和App_CategoryModel(指定AppModel和CategoryModel之间的多对多关系)。 这个片段是:

public class CategoryModel { [Key] public int id { get; set; } public string Name {get; set; } public virtual ICollection mapping { get; set; } } public class AppModel { [Key] public int id { get; set; } public string Name { get; set; } public virtual ICollection mapping { get; set; } } public class App_CategoryModel { [Key] public int id {get; set;} public int AppId {get; set; } public int CategoryId {get; set; } public virtual AppModel App {get; set;} public virtual CategoryModel Category {get; set;} } 

我遵循’代码优先’的方法,并成功创建表。 但是,现在我被困在如何填充和显示这些信息。 我有以下输入数据: List ListDictionary<"appname", List>

我如何从这里继续前进,以便我可以更新映射表?

此外,想要了解这是否是表示数据的正确方法。 由于应用程序可以有多个类别 – 我希望输出作为唯一应用程序的集合以及每个应用程序的类别列表,例如:

 Dictionary<AppModel, List> 

编辑:这是我根据smoksnes的建议尝试的 –

  List cat_list = new List(); CategoryModel c1 = new CategoryModel(); c1.Name = "string1"; cat_list.Add(c1); CategoryModel c2 = new CategoryModel(); c2.Name = "string2"; cat_list.Add(c2); List app_list = new List(); AppModel a1 = new AppModel(); a1.Name = "app1"; app_list.Add(a1); AppModel a2 = new AppModel(); a2.Name = "app2"; app_list.Add(a2); a1.mapping.Add(c1); a1.mapping.Add(c2); a2.mapping.Add(c1); a2.mapping.Add(c2); db.categories.AddRange(cat_list); db.apps.AddRange(app_list); db.SaveChanges(); 

在此之后,EF作为已完成的工作 – 在映射表中有2个类别,2个应用程序和4个条目。

虽然这有效,但不确定是谁停止EF为类别创建4个条目?

正如Barry O’Kane在评论中提到的那样,没有理由保留App_CategoryModel模型。 EF将为您管理此事。 如果它包含有关两个表之间关系的任何额外信息,则应该保留它。 但根据你的例子,没有理由保留它。

 public class CategoryModel { public CategoryModel() { AppModels = new List(); } [Key] public int id { get; set; } public string Name {get; set; } public virtual ICollection AppModels { get; set; } } public class AppModel { public AppModel() { // Not sure if this is actually needed anymore. But EF usually adds it. CategoryModels = new List(); } [Key] public int id { get; set; } public string Name { get; set; } public virtual ICollection CategoryModels { get; set; } } 

关于你的代表问题,我认为没有必要。 由于AppModel已经在其模型上具有连接的CategoryModel ,因此没有理由使用Dictionary 。 您可以将其存储在List

 IList myApps = context.AppModels.ToList(); foreach (var myApp in myApps) { Console.Writeline("App {0} has the following categories:", myApp.id); foreach (var category in myApp.CategoryModels) { Console.Writeline(category.Name); } } 

当您想要为应用添加类别时:

 // I don't know how you create your Context, so below it's just called context. var newCategoryModel = new CategoryModel { Name = "SO is awesome!" }; var appModel = context.AppModels.FirstOrDefault(x => x.id == 1); appModel.CategoryModels.Add(newCategoryModel); // EF will automatically set foreign keys for you... context.SaveChanges(); 

如果您想确保没有添加两次类别:

 public void AddCategory(int appId, string categoryName) { using(var context = new DbContext()) { var category = context.CategoryModels.FirstOrDefault(x => x.Name == categoryName); if(category == null) { // Only create new CategoryModel if it doesn't exist. category = new CategoryModel { Name = categoryName }; } var appModel = new AppModel { id = appId }; // Attach to save on db-trip context.AppModels.Attach(appModel); //TODO: Possibly check if this particular appModel already has this category? appModel.CategoryModels.Add(category); context.SaveChanges(); } }