AutoMapper.Collections.EntityFramework
AutoMapper.Collections.EntityFramework
用不同的方式问同样的问题!
很明显我需要详细说明这个问题,因为我没有可行的答案。
基于此AutoMapper注册码:
Mapper.Initialize(cfg =>
{
cfg.AddCollectionMappers();
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DbContext>>();
});
AutoMapper 使用此行添加了对 "updating" DbSet 集合的支持:
Mapper.Map<List<DTO>, List<Entity>>(dtoCollection, entityCollection);
通过打开的上下文保存更改应该会导致更新数据库:
using (var context = factory.CreateContext())
{
Mapper.Map<List<DTO>, List<Entity>>(dtoCollection, await
context.dbSet.TolistAsync());
await context.SaveChangesAsync();
}
这没有任何作用!
回到我原来的问题。如果使用 dto 和实体集合的当前状态调用映射器 returns 更新的实体集合基于比较映射在这里创建:
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DbContext>>();
在此处生成实体集合:
var entities = Mapper.Map<List<DTO>, List<Entity>>(dtoCollection, await
context.dbSet.TolistAsync());
我是否支持使用这个新集合迭代新集合并手动更新 EF?目前尚不清楚我应该做什么?这就是我应该对生成的集合所做的吗?
// map dto's to entities
var entities = Mapper.Map(collection, await dbSet.ToListAsync());
// add new records
var toAdd = entities.Where(e => e.Id == 0);
dbSet.AddRange(toAdd);
// delete old records
var toDelete = entities.Where(entity => collection.All(e => e.Id > 0 && e.Id != entity.Id));
dbSet.RemoveRange(toDelete);
// update existing records
var toUpdate = entities.Where(entity => collection.All(i => i.Id > 0 && i.Id == entity.Id)).ToList();
foreach (var entity in toUpdate)
{
context.Entry(entity).State = EntityState.Modified;
}
await context.SaveChangesAsync();
这是我原来的问题。如果是这样,那似乎是多余的。所以我觉得我错过了什么。
我感谢一些有用的反馈。请帮忙!
谢谢
EF DbSet
s 不是集合。基本上它们代表一个数据库 table 并为其提供查询和 DML 操作。
您似乎想要将整个 table 与 DTO 列表同步。您可以通过使用 Load
或 LoadAsync
方法在本地加载整个 table 来做到这一点,然后 Map
将 DTO 集合加载到实体 DbSet.Local
属性.与您尝试的不同之处在于 Local
属性 不是一个简单的列表,而是直接绑定到上下文本地存储和更改跟踪器的可观察集合,因此任何修改(Add
、Remove
) 将应用于数据库。
像这样:
await dbSet.LoadAsync();
Mapper.Map(dtoCollection, dbSet.Local);
await context.SaveChangesAsync();
用不同的方式问同样的问题!
很明显我需要详细说明这个问题,因为我没有可行的答案。
基于此AutoMapper注册码:
Mapper.Initialize(cfg =>
{
cfg.AddCollectionMappers();
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DbContext>>();
});
AutoMapper 使用此行添加了对 "updating" DbSet 集合的支持:
Mapper.Map<List<DTO>, List<Entity>>(dtoCollection, entityCollection);
通过打开的上下文保存更改应该会导致更新数据库:
using (var context = factory.CreateContext())
{
Mapper.Map<List<DTO>, List<Entity>>(dtoCollection, await
context.dbSet.TolistAsync());
await context.SaveChangesAsync();
}
这没有任何作用!
回到我原来的问题。如果使用 dto 和实体集合的当前状态调用映射器 returns 更新的实体集合基于比较映射在这里创建:
cfg.SetGeneratePropertyMaps<GenerateEntityFrameworkPrimaryKeyPropertyMaps<DbContext>>();
在此处生成实体集合:
var entities = Mapper.Map<List<DTO>, List<Entity>>(dtoCollection, await
context.dbSet.TolistAsync());
我是否支持使用这个新集合迭代新集合并手动更新 EF?目前尚不清楚我应该做什么?这就是我应该对生成的集合所做的吗?
// map dto's to entities
var entities = Mapper.Map(collection, await dbSet.ToListAsync());
// add new records
var toAdd = entities.Where(e => e.Id == 0);
dbSet.AddRange(toAdd);
// delete old records
var toDelete = entities.Where(entity => collection.All(e => e.Id > 0 && e.Id != entity.Id));
dbSet.RemoveRange(toDelete);
// update existing records
var toUpdate = entities.Where(entity => collection.All(i => i.Id > 0 && i.Id == entity.Id)).ToList();
foreach (var entity in toUpdate)
{
context.Entry(entity).State = EntityState.Modified;
}
await context.SaveChangesAsync();
这是我原来的问题。如果是这样,那似乎是多余的。所以我觉得我错过了什么。
我感谢一些有用的反馈。请帮忙!
谢谢
EF DbSet
s 不是集合。基本上它们代表一个数据库 table 并为其提供查询和 DML 操作。
您似乎想要将整个 table 与 DTO 列表同步。您可以通过使用 Load
或 LoadAsync
方法在本地加载整个 table 来做到这一点,然后 Map
将 DTO 集合加载到实体 DbSet.Local
属性.与您尝试的不同之处在于 Local
属性 不是一个简单的列表,而是直接绑定到上下文本地存储和更改跟踪器的可观察集合,因此任何修改(Add
、Remove
) 将应用于数据库。
像这样:
await dbSet.LoadAsync();
Mapper.Map(dtoCollection, dbSet.Local);
await context.SaveChangesAsync();