我通过API获取DTO列表。
然后我将它们映射到实体。
DTO看起来像:
public class BrandDTO
{
public int Id { get; set; }
public string Name { get; set; }
public bool Deleted { get; set; }
public static Brand MapToModel(BrandDTO model)
{
var retModel = new Brand
{
Name = model.Name,
Deleted = model.Deleted,
BrandId = model.Id
};
return retModel;
}
public static IList<Brand> MapToModel(IEnumerable<BrandDTO> models)
{
return models.Select(MapToModel).ToList();
}
}
实体看起来像:
public class Brand
{
public int Id { get; set; }
public bool Deleted { get; set; }
public int BrandId { get; set; }
public string Name { get; set; }
}
我如何从DTO(品牌输入参数)获得模型:
var brandsModels = BrandDTO.MapToModel(brands);
var updatedBrands = brandsModels.Where(b => existingBrandsIds.Contains(b.BrandId));
await repo.Update(updatedBrands);// which just make await context.SaveChangesAsync();
据我了解,实体未更新,因为我没有在上下文中“更新”实体与实体。我对吗?如果是这样我怎么能这样做,是否有办法收集实体?