很抱歉我的英语很差。
我有ASP。使用实体框架的。NET Core Web API。对于我的一个方法,我想返回一个具有所有关系(一对一)的对象。
我的UserAccount
类是:
[Table("UserAccount")]
public class UserAccount : BaseClass
{
// Foreign Keys
[ForeignKey(nameof(UserAccountType))]
public int UserAccountTypeId { get; set; }
[ForeignKey(nameof(Gender))]
public int GenderId { get; set; }
[ForeignKey(nameof(Truck))]
public int? TruckId { get; set; }
// Properties
public string LastName { get; set; }
public string FirstName { get; set; }
public string UserName { get; set; }
[DataType(DataType.EmailAddress)]
public string Mail { get; set; }
public string Login { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
// Navigation Properties
[IgnoreDataMember]
public virtual Gender Gender { get; set; }
[IgnoreDataMember]
public virtual UserAccountType UserAccountType { get; set; }
[IgnoreDataMember]
public virtual Truck Truck { get; set; }
}
我还使用了DTO类:
public class UserAccountDto : BaseClassDto
{
// Foreign Keys
public int UserAccountTypeId { get; set; }
public int GenderId { get; set; }
public int TruckId { get; set; }
// Properties
public string LastName { get; set; }
public string FirstName { get; set; }
public string UserName { get; set; }
public string Mail { get; set; }
public string Login { get; set; }
public string Password { get; set; }
// Linked Objects
public virtual GenderDto Gender { get; set; }
public virtual UserAccountTypeDto UserAccountTypes { get; set; }
public virtual TruckDto Trucks { get; set; }
}
我想接收Gender
,UserAccountType
和Truck
的所有关系对象。
[HttpGet("GetByTruck/{truckId}", Name = "UserAccountByTruckId")]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<UserAccount>> GetByTruckId(int truckId)
{
if (truckId <= 0)
return BadRequest();
try
{
var userAccounts = await _repository.UserAccount.GetByTruckIdAsync(truckId);
var userAccountsResult = _mapper.Map<IEnumerable<UserAccountDto>>(userAccounts);
if (userAccountsResult == null)
return NotFound();
return Ok(userAccountsResult);
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside GetById action int UserAccountController : {ex.Message} ");
return StatusCode(StatusCodes.Status500InternalServerError, "Internal Server Error");
}
}
在这个方法中,属性包含所有对象,但是当我尝试将结果映射到Dto类时,我丢失了关系对象。
所以我只有一个UserAccount对象,没有其他对象,如性别,UserAccountType或卡车。该对象变为Null。有人能帮我吗?
感谢谢谢@ranton187你救了我!希望能帮助别人,这里是我解决问题的方法。我在映射配置文件中添加了这个:
CreateMap相关内容
最新更新
- 将十进制转换为十六进制,并确保它们是数字
- 为用户在登录页面后添加路由
- 动态变量导致错误,因为它没有正确分配
- 有可能从手机中获得指纹或人脸验证吗?
- File IO From a JAR File
- 如何获得上下文在一个简单的类没有BuildContext在扑动
- 无法向 Tkinter 窗口添加背景
- 如何将RGB图像转换为灰度,但保持一种颜色?——Java
- 我应该在Docker内部还是在虚拟环境中安装气流?
- 属性错误: 'DataFrame'对象没有属性'assign'
- 如何在每次在DirectoryInfo.GetFiles()中加载新文件时执行操作?
- HTML中的Javascript在Angular中不起作用
- 如何在firebase中调度大约每整小时运行一次的函数?
- Uncaught TypeError:不能访问数组上字符串类型的偏移量
- Switch(Select)在TRANSFORM和Select之间工作吗?
- MySQL SELECT查询工作,但具有相同WHERE子句的UPDATE查询不工作
- drupal 10 localhost站点:如何在安装和注销后访问登录页面
- Flutter原生启动画面图像未出现- Flutter (Dart)
- 用另一个远程分支覆盖一个远程分支
- 邮递员说"The route api...could not be found."(拉拉维尔)
- Firebase Messaging项目Google Analytics升级失败
- 在初始页面加载时未应用FacetWP查询顺序数组设置
- GitHub邀请过期吗?
- 使用AWS秘密管理器端点的安全组
- 如何做基于名称和值的点击事件?
- 在容器和主机上创建具有相同Linux用户id的用户
- Firebase身份验证在部署后不工作.扑动Web应用程序显示空白的白色屏幕
- AWS Client VPN Access Public URL/IP
- python dict get方法即使key在dict中也运行第二个参数
- 如何在Python中使用每周时间序列
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium