使用实体框架

时间:2016-05-09 11:54:03

标签: c# entity-framework ef-code-first

我正在学习asp.net mvc应用程序中的实体框架。我有3个型号 -

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

public class CategoryModel
    {
        [Key]
        public int id { get; set; }
        public string Name {get; set; }
        public virtual ICollection<App_CategoryModel> mapping { get; set; }
    }

    public class AppModel
    {
        [Key]
        public int id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<App_CategoryModel> 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;}
    }

我关注的是“代码优先”&#39;方法和表格已成功创建。但是,现在我被困在如何填充和显示这些信息。 我有以下作为输入数据:  List<AppModel>  List<CategoryModel>Dictionary<"appname", List<CategoryModel>>

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

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

Dictionary<AppModel, List<CategoryModel>>

修改 这是我根据smoksnes的建议尝试的 -

    List<CategoryModel> cat_list = new List<CategoryModel>();
    CategoryModel c1 = new CategoryModel();
    c1.Name = "string1";
    cat_list.Add(c1);

    CategoryModel c2 = new CategoryModel();
    c2.Name = "string2";
    cat_list.Add(c2);

    List<AppModel> app_list = new List<AppModel>();
    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个条目?

1 个答案:

答案 0 :(得分:1)

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

public class CategoryModel
{
    public CategoryModel()
    {
        AppModels = new List<AppModel>();
    }

    [Key]
    public int id { get; set; }
    public string Name {get; set; }
    public virtual ICollection<AppModel> AppModels { get; set; }
}

public class AppModel
{
    public AppModel()
    {
        // Not sure if this is actually needed anymore. But EF usually adds it.
        CategoryModels = new List<CategoryModel>();
    }

    [Key]
    public int id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<CategoryModel> CategoryModels { get; set; }
}

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

IList<AppModel> 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();
    }
}