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

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

问题描述

我正在使用EF4和新的。我的项目有很多到很多,似乎无法解决如何插入或更新。我已经建立一个小项目,只是为了看看它应该如何编码。



假设我有3个表


  1. 类:ClassID-ClassName

  2. 学生:StudentID-FirstName-Surname

  3. StudentClass:StudentID-ClassID

添加所有关系并通过模型浏览器更新模型后,我注意到StudentClass没有出现,这似乎是默认行为。 >

现在我需要同时执行插入和更新。你怎么做呢?任何代码示例或链接,我可以下载一个例子,或者你可以休息5分钟?

解决方案

对象),您有一个对象,其中包含学生学生对象,其集合为。由于您的 StudentClass 表只包含Ids,没有额外的信息,EF不会为连接表生成一个实体。这是正确的行为,这就是你的期望。



现在,当插入或更新时,尝试考虑对象。例如。如果要插入一个有两个学生的课程,请创建 Class 对象, Student 对象,将学生添加到类学生集合将对象添加到上下文中,并调用 SaveChanges
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $新Class {Name =Math};
mathClass.Students.Add(new Student {Name =Alice});
mathClass.Students.Add(new Student {Name =Bob});

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

这将在 Class 表, Student 表中的两个条目和 StudentClass 表中的两个条目链接在一起。



您基本上做同样的更新。只需获取数据,通过添加和删除集合中的对象来修改图形,调用 SaveChanges 。有关详细信息,请查看此类似问题



修改



根据您的评论,您需要插入一个新的 Class 并添加两个现有的学生

使用(var context = new YourContext())
{
var mathClass = new Class {Name =Math};
学生student1 = context.Students.FirstOrDefault(s => s.Name ==Alice);
学生student2 = context.Students.FirstOrDefault(s => s.Name ==Bob);
mathClass.Students.Add(student1);
mathClass.Students.Add(student2);

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

既然这两个学生都已经在数据库中,但是由于它们现在位于学生收集 Class 中,所以两个条目将被插入 StudentClass 表。


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.

Suppose I have 3 tables

  1. Class : ClassID-ClassName
  2. Student : StudentID-FirstName-Surname
  3. StudentClass : StudentID-ClassID

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.

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?

解决方案

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.

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

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.

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.

Edit:

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

Since both students are already in the database, they won't be inserted, but since they are now in the Students collection of the Class, two entries will be inserted into the StudentClass table.

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

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