다 대다 Entity Framework 삽입 / 업데이트. 어떻게하나요?
EF4를 사용하고 있으며 처음 사용하고 있습니다. 내 프로젝트에 다대 다가 있고 삽입 또는 업데이트 방법을 해결할 수없는 것 같습니다. 코딩 방법을보기 위해 작은 프로젝트를 빌드했습니다.
테이블이 3 개 있다고 가정 해 보겠습니다.
- 클래스 : ClassID-ClassName
- 학생 : StudentID-FirstName-Surname
- StudentClass : StudentID-ClassID
모든 관계를 추가하고 모델 브라우저를 통해 모델을 업데이트 한 후 StudentClass가 나타나지 않는 것으로 나타났습니다. 이것이 기본 동작 인 것 같습니다.
이제 삽입과 업데이트를 모두 수행해야합니다. 어떻게하나요? 예제를 다운로드 할 수있는 코드 샘플이나 링크가 있습니까? 아니면 5 분 정도 여유가 있습니까?
엔터티 (또는 개체)의 관점 Class에서 컬렉션이 Students있는 Student개체 와 컬렉션이 있는 개체가 있습니다 Classes. 당신 때문에 StudentClass테이블에만 ID와 별도의 정보를 포함, EF는 가입 테이블에 대한 엔티티를 생성하지 않습니다. 그것은 올바른 행동이며 당신이 기대하는 것입니다.
이제 삽입이나 업데이트를 할 때 객체의 관점에서 생각하십시오. 예를 들어 두 명의 학생이있는 클래스를 삽입하려면 Class객체, Student객체를 만들고 학생을 클래스 Students컬렉션에 추가 Class하고 컨텍스트에 객체를 추가 하고 다음을 호출합니다 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();
}
이것은에 항목이 만들어집니다 Class테이블의 두 엔트리 Student테이블과 두 개의 항목 StudentClass들을 함께 연결 테이블을.
기본적으로 업데이트에 대해 동일하게 수행합니다. 데이터를 가져오고 컬렉션에서 개체를 추가 및 제거하여 그래프를 수정하고 SaveChanges. 자세한 내용은 이 유사한 질문 을 확인 하십시오.
편집 :
귀하의 의견에 따라 새 항목을 삽입하고 Class두 개의 기존 항목 Students을 추가 해야합니다.
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();
}
두 학생이 이미 데이터베이스에 있으므로 삽입되지 않지만 이제 Students.NET 컬렉션에 있으므로 Class두 항목이 StudentClass테이블에 삽입됩니다 .
업데이트를 위해 이것을 시도하십시오.
[HttpPost]
public ActionResult Edit(Models.MathClass mathClassModel)
{
//get current entry from db (db is context)
var item = db.Entry<Models.MathClass>(mathClassModel);
//change item state to modified
item.State = System.Data.Entity.EntityState.Modified;
//load existing items for ManyToMany collection
item.Collection(i => i.Students).Load();
//clear Student items
mathClassModel.Students.Clear();
//add Toner items
foreach (var studentId in mathClassModel.SelectedStudents)
{
var student = db.Student.Find(int.Parse(studentId));
mathClassModel.Students.Add(student);
}
if (ModelState.IsValid)
{
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mathClassModel);
}
I wanted to add my experience on that. Indeed EF, when you add an object to the context, it changes the state of all the children and related entities to Added. Although there is a small exception in the rule here: if the children/related entities are being tracked by the same context, EF does understand that these entities exist and doesn't add them. The problem happens when for example, you load the children/related entities from some other context or a web ui etc and then yes, EF doesn't know anything about these entities and goes and adds all of them. To avoid that, just get the keys of the entities and find them (e.g. context.Students.FirstOrDefault(s => s.Name == "Alice")) in the same context in which you want to do the addition.
I use the following way to handle the many-to-many relationship where only foreign keys are involved.
So for inserting:
public void InsertStudentClass (long studentId, long classId)
{
using (var context = new DatabaseContext())
{
Student student = new Student { StudentID = studentId };
context.Students.Add(student);
context.Students.Attach(student);
Class class = new Class { ClassID = classId };
context.Classes.Add(class);
context.Classes.Attach(class);
student.Classes = new List<Class>();
student.Classes.Add(class);
context.SaveChanges();
}
}
For deleting,
public void DeleteStudentClass(long studentId, long classId)
{
Student student = context.Students.Include(x => x.Classes).Single(x => x.StudentID == studentId);
using (var context = new DatabaseContext())
{
context.Students.Attach(student);
Class classToDelete = student.Classes.Find(x => x.ClassID == classId);
if (classToDelete != null)
{
student.Classes.Remove(classToDelete);
context.SaveChanges();
}
}
}
In entity framework, when object is added to context, its state changes to Added. EF also changes state of each object to added in object tree and hence you are either getting primary key violation error or duplicate records are added in table.
'Programing' 카테고리의 다른 글
| 와일드 카드 또는 "like"와 유사한 디렉토리 이름 찾기 (0) | 2020.08.18 |
|---|---|
| Spring Boot JPA-자동 재 연결 구성 (0) | 2020.08.18 |
| Node.js, Express 및 Mongoose를 사용하여 이미지 업로드 (0) | 2020.08.18 |
| Go에서 들여 쓰기 : 탭 또는 공백? (0) | 2020.08.18 |
| C ++에서 명령문 순서 적용 (0) | 2020.08.18 |