问题描述
我有一个json对象的电影
,我传递给我的控制器。
{data:[{
pre>
Id:12345,
标题:'Movie1',
年:2010,
字符:[{
名称:'Character1',
个人:{Id:1,Name:'Person1'}
},{
名称:'Character2',
个人:{Id:2,Name:'Person2'}
}]
}]}
模型绑定工作正常,但我有一个问题,通过实体将每个
电影
插入数据库框架5.当插入电影的每个Person
时,我遇到主键违规。
我了解为什么会这样发生。添加新的
电影
时,有些人已经存在于数据库中,以便发生违规。每个人的Id不是自动生成的。我正在使用我从api收到的Id,我得到的信息。
A
电影
与Person $ c有多对多关系$ c>,
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 eachPerson
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 withPerson
, withCharacter
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 andAttach()
if it exists orAdd()
if it doesn't. Can Entity Framework automatically handle inserting a newPerson
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中插入具有多对多关系的新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!