插入/更新多对多实体框架。我该怎么做? [英] Insert/Update Many to Many Entity Framework . How do I do it?

查看:105
本文介绍了插入/更新多对多实体框架。我该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF4和新来的。我有一个多对多在我的项目,似乎无法工作,如何插入或更新。我建一个小的项目只是为了看看应该如何codeD。

I'm using EF4 and new to it. I have a many to many in my project and cannot seem to work out how to insert or update. I have build a small project just to see how it should be coded.

假设我有3个表


  1. 类:的ClassID-类名

  2. 学生:StudentID-名字 - 姓氏

  3. StudentClass:StudentID-的ClassID

将所有的关系,并通过更新浏览器的模型我已经注意到,StudentClass没有出现​​在模型之后,这似乎是默认行为。

After adding all the relationship and updated the model via model browser I have noticed that StudentClass does not appear, this seems to be default behaviour.

现在我需要做既是插入和更新。你是怎么做到的呢?任何code样品或链接在哪里可以下载一个例子,或者您能抽出5分钟?

Now I need to do both an Insert and Update. How do you do it? Any code samples or link where I can download an example, or can you spare 5 mins?

推荐答案

在实体(或对象),你必须拥有的集合对象而言学生学生对象具有类的集合。由于您的 StudentClass 表只包含的ID并没有多余的信息,EF不会生成的连接表的实体。这是正确的行为,这就是你所期望的。

In terms of entities (or objects) you have a Class object which has a collection of Students and a Student object that has a collection of Classes. Since your StudentClass table only contains the Ids and no extra information, EF does not generate an entity for the joining table. That is the correct behaviour and that's what you expect.

现在,做插入或更新时,尽量想从对象的角度。例如。如果要插入一个班有两个学生,创建对象时,<​​code>学生对象,添加学生类学生集合对象添加到上下文并调用的SaveChanges

Now, when doing inserts or updates, try to think in terms of objects. E.g. if you want to insert a class with two students, create the Class object, the Student objects, add the students to the class Students collection add the Class object to the context and call SaveChanges:

using (var context = new YourContext())
{
    var mathClass = new Class { Name = "Math" };
    mathClass.Students.Add(new Student { Name = "Alice" });
    mathClass.Students.Add(new Student { Name = "Bob" });

    context.AddToClasses(mathClass);
    context.SaveChanges();
}

这将创建在表中的条目,在学生表两个条目,并在两个条目 StudentClass 表连接在一起。

This will create an entry in the Class table, two entries in the Student table and two entries in the StudentClass table linking them together.

您基本上做到更新一样。只是提取数据,从集合添加和删除对象修改图,叫的SaveChanges 。检查<一href=\"http://stackoverflow.com/questions/3612477/entity-framework-4-many-to-many-update/3622599#3622599\">this类似的问题的详情。

You basically do the same for updates. Just fetch the data, modify the graph by adding and removing objects from collections, call SaveChanges. Check this similar question for details.

修改

根据您的意见,您需要插入一个新的并添加两个现有的学生它:

According to your comment, you need to insert a new Class and add two existing Students to it:

using (var context = new YourContext())
{
    var mathClass= new Class { Name = "Math" };
    Student student1 = context.Students.FirstOrDefault(s => s.Name == "Alice");
    Student student2 = context.Students.FirstOrDefault(s => s.Name == "Bob");
    mathClass.Students.Add(student1);
    mathClass.Students.Add(student2);

    context.AddToClasses(mathClass);
    context.SaveChanges();
}

由于这两个学生已经在数据库中,他们不会被插入,但因为他们现在是类的<​​code>学生集合,两个条目将被插入 StudentClass 表。

这篇关于插入/更新多对多实体框架。我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆