c# - 如何在实体框架的 .Add() 中传递 DTO?
问题描述
例如
我有一个实体
学生
ID, Name, DateCreated, GUID
学生DTO
Name, DateCreated
现在自动映射器
CreateMap<students, studentsDTO>()
.ForSourceMember(up=> up.ID, opt=> opt.Ignore())
.ForSourceMember(up => up. GUID, opt=> opt.Ignore());
现在我有一个方法
public IHttpActionResult AddStudents(studentsDTO model)
{
_context.Students.Add(model);
return Ok();
}
但抛出类型model
与预期类型不匹配的错误Add
。
我该如何解决?
解决方案
在添加之前,您必须将 DTO 映射到您的实体,如下所示:
public IHttpActionResult AddStudents(studentsDTO model)
{
_context.Students.Add(_mapper.Map<Students>(model));
return Ok();
}
推荐阅读
- node.js - bcrypt 不适用于 lambda 无服务器
- java - 2019-06-17T20:27:23.706000000Z 的日期格式是什么?
- r - R中的条件分组
- postgresql - 如何将 3MB txt 文件逐行加载到 PostgreSQL 中的列?
- suitescript2.0 - 如何根据同一行上的其他子列表值设置当前行上子列表字段的值。我正在使用客户端脚本
- laravel - 未捕获(承诺中)错误:product_categories.name 不是检索到的构面
- c - C程序创建一个简单的计算器
- android - 具有多对多关系的房间数据库:在查询过滤数据时返回单个条目
- android - 如何使用 GSON 实现 json?得到一个错误
- python - 新对象不会追加到列表