Framework中插入具有多对多关系的新对象

Framework中插入具有多对多关系的新对象

本文介绍了在Entity Framework中插入具有多对多关系的新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json对象的电影,我传递给我的控制器。

  {data:[{
Id:12345,
标题:'Movie1',
年:2010,
字符:[{
名称:'Character1',
个人:{Id:1,Name:'Person1'}
},{
名称:'Character2',
个人:{Id:2,Name:'Person2'}
}]
}]}
pre>

模型绑定工作正常,但我有一个问题,通过实体将每个电影插入数据库框架5.当插入电影的每个 Person 时,我遇到主键违规。



我了解为什么会这样发生。添加新的电影时,有些人已经存在于数据库中,以便发生违规。每个人的Id不是自动生成的。我正在使用我从api收到的Id,我得到的信息。



A 电影 Person Character 是两者之间的桥梁。

  public class Movie {
public int Id {get;组; }
public string标题{get;组; }
public int?年{get;组; }
public virtual ICollection< Character>人物{get;组; }

public Movie(){
Characters = new HashSet< Character>();
}
}

public class Character {
public string Name {get;组; }
public virtual Movie Movie {get;组; }
public virtual Person Person {get;组; }
}

public class Person {
public int Id {get; set; }
public string Name {get;组; }
public virtual ICollection< Character>人物{get;组; }

public Person(){
Characters = new HashSet< Character>();
}
}

我的问题是...我知道我可以循环通过每个 Person 对象和 Attach()(如果它存在)或 Add()如果没有。 可以实体框架自动处理插入新的 Person 并忽略它(如果它已经存在)如果没有,这种类型的问题最好的做法是什么?

解决方案

我认为没有必要附加Person对象,如果它们已经存在。



我会做的是循环遍历API Person对象并与EF DbContext进行比较。

  var dbPerson = MyMovieContext.Persons.Where(x => x.ID == myPersonID).SingleOrDefault(); 

如果找不到匹配项,则添加。如果你找到一个匹配项,然后用DbContext替换Character上的Entity引用。


I have a json object of Movies that I'm passing to my controller.

{data : [{
    Id: 12345,
    Title: 'Movie1',
    Year: 2010,
    Character: [{
        Name: 'Character1',
        Person: { Id: 1, Name: 'Person1' }
    },{
        Name: 'Character2',
        Person: { Id: 2, Name: 'Person2' }
    }]
}]}

The model binding is working fine but I'm having an issue inserting each Movie into the database via Entity Framework 5. I'm getting a primary key violation when each Person for a movie is being inserted.

I understand why this is happening. Some people already exist in the database when adding a new Movie so the violation occurs. The Id for each person is not auto generated. I'm using the Id that I receive from the api I get the information from.

A Movie has a many-to-many relationship with Person, with Character being the bridge between the two.

public class Movie {
    public int Id { get; set; }
    public string Title { get; set; }
    public int? Year { get; set; }
    public virtual ICollection<Character> Characters { get; set; }

    public Movie() {
        Characters = new HashSet<Character>();
    }
}

public class Character {
    public string Name { get; set; }
    public virtual Movie Movie { get; set; }
    public virtual Person Person { get; set; }
}

public class Person {
    public int Id { get;set; }
    public string Name { get; set; }
    public virtual ICollection<Character> Characters { get; set; }

    public Person() {
        Characters = new HashSet<Character>();
    }
}

My question is... I know I can loop through each Person object and Attach() if it exists or Add() if it doesn't. Can Entity Framework automatically handle inserting a new Person and ignore it if it already exists? If not, what is the best practice for this type of problem?

解决方案

I believe it is unnecessary to Attach the Person objects if they already exist.

What I would do is loop through the API Person objects and compare with the EF DbContext.

var dbPerson = MyMovieContext.Persons.Where(x => x.ID == myPersonID).SingleOrDefault();

If you don't find a match, then Add. If you do find a match, then replace the reference on Character with the Entity from the DbContext.

这篇关于在Entity Framework中插入具有多对多关系的新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:58