是否可以使用自动映射器更新多对多关系?
Is it possible to update Many to Many relation using automapper?
我在 2 table 之间有一个多对多的关系,如下例所示:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class PersonVm
{
public int Id { get; set; }
public string Name {get;set}
public int[] BookIds { get; set; }
public ICollection<Book> Books=>_context.Books.Join(BookIds,x=>x.Id,y=>y,(x,y)=>x);
}
然后我有这样的映射:
map.CreateMap<PersonVm, Person>()
.ForMember(x => x.Books, a => a.MapFrom(b => b.BookIds));
以及我用于插入或更新的方法:
public void Update(PersonVm item)
{
using (IUnitOfWork db = new UnitOfWork(new ApplicationDbContext()))
{
var oldItem = db.Persons.Get(item.Id);
var newItem = Mapper.Map<Person>(item);
if (oldItem == null)
{
db.Persons.Add(newItem);
}
else
{
db.Persons.Update(oldItem, newItem);
}
db.Complete();
}
}
使用此代码,我可以到达 "SaveChanges" 点,在那里我收到一条错误消息,提示我不允许在图书 table 中插入重复项(图书 table 设置为不允许重复——这并不奇怪)。
问题是为什么 entity framework 试图插入书籍?
我可以告诉 EF
不要将书插入书 table 吗?
我只想插入 Person 并将数据插入 Person-Books table。
在我的例子中人不存在,必须插入,书籍存在。
如有任何建议,我们将不胜感激。
编辑:关系
public class PersonMap : EntityTypeConfiguration<Person>
{
public PersonMap()
{
HasMany(x => x.Books)
.WithMany(x => x.Persons);
}
}
和反向映射:
map.CreateMap<Person, PersonVm>()
.ForMember(x => x.BookIds, a => a.MapFrom(b => b.Books.Select(c => c.Id).ToArray()));
当您使用地图中的书籍时,它们不会附加。所以 EF 不知道它们已经存在于数据库中。遍历所有书籍并将它们附加到上下文中。例如:
using(var context = new ApplicationDbContext())
{
foreach(var book in person.Books)
context.Books.Attach(book);
}
提示:当 item.Id 等于 0 时,它不存在于数据库中。
我在 2 table 之间有一个多对多的关系,如下例所示:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class PersonVm
{
public int Id { get; set; }
public string Name {get;set}
public int[] BookIds { get; set; }
public ICollection<Book> Books=>_context.Books.Join(BookIds,x=>x.Id,y=>y,(x,y)=>x);
}
然后我有这样的映射:
map.CreateMap<PersonVm, Person>()
.ForMember(x => x.Books, a => a.MapFrom(b => b.BookIds));
以及我用于插入或更新的方法:
public void Update(PersonVm item)
{
using (IUnitOfWork db = new UnitOfWork(new ApplicationDbContext()))
{
var oldItem = db.Persons.Get(item.Id);
var newItem = Mapper.Map<Person>(item);
if (oldItem == null)
{
db.Persons.Add(newItem);
}
else
{
db.Persons.Update(oldItem, newItem);
}
db.Complete();
}
}
使用此代码,我可以到达 "SaveChanges" 点,在那里我收到一条错误消息,提示我不允许在图书 table 中插入重复项(图书 table 设置为不允许重复——这并不奇怪)。
问题是为什么 entity framework 试图插入书籍?
我可以告诉 EF
不要将书插入书 table 吗?
我只想插入 Person 并将数据插入 Person-Books table。
在我的例子中人不存在,必须插入,书籍存在。
如有任何建议,我们将不胜感激。
编辑:关系
public class PersonMap : EntityTypeConfiguration<Person>
{
public PersonMap()
{
HasMany(x => x.Books)
.WithMany(x => x.Persons);
}
}
和反向映射:
map.CreateMap<Person, PersonVm>()
.ForMember(x => x.BookIds, a => a.MapFrom(b => b.Books.Select(c => c.Id).ToArray()));
当您使用地图中的书籍时,它们不会附加。所以 EF 不知道它们已经存在于数据库中。遍历所有书籍并将它们附加到上下文中。例如:
using(var context = new ApplicationDbContext())
{
foreach(var book in person.Books)
context.Books.Attach(book);
}
提示:当 item.Id 等于 0 时,它不存在于数据库中。