公司 ID 在 API 调用中返回 0

Company Id returning 0 on API call

大家晚上好,

我目前正在编写 API 调用以在我的数据库中创建一个新公司。我有:

  1. 一个 AppUser class
public int Id { get; set; }
public string UserName { get; set; }
public virtual ICollection<AppUserCompanySetting> AppUserCompanySettings { get; set; }  = new List<AppUserCompanySetting>();
  1. 公司设置class
public class CompanySetting
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public string CompanyRegistrationNumber { get; set; }
        public bool isActive { get; set; }
        public bool isArchived { get; set; }
        public virtual ICollection<AppUserCompanySetting> AppUserCompanySettings { get; set; } = new List<AppUserCompanySetting>();
    }
  1. A Join table 两个实体 (AppUserCompanySetting.cs):
public class AppUserCompanySetting
    {
        public int AppUserId { get; set; }
        public virtual AppUser AppUser { get; set; }
        public int CompanySettingsId { get; set; }
        public virtual CompanySetting CompanySettings { get; set; }
    }
  1. 公司的 DTO
public class CompanyDto
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public string CompanyRegistrationNumber { get; set; }
        public bool isActive { get; set; }
        public bool isArchived { get; set; }
        public int UserId { get; set; }
    }
  1. 从 CompanySetting 到 CompanyDto 的 AutoMapper
CreateMap<CompanySetting, CompanyDto>()
                .ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.AppUserCompanySettings.Select(y => y.AppUserId).FirstOrDefault()));
  1. 我创建公司的方法:
public async Task<ActionResult<CompanyDto>> AddNewCompany(CompanyDto companyDto)
        {
            var user = await _context.Users
                .SingleOrDefaultAsync(x => x.Id == companyDto.UserId);

            var company = new CompanySetting
            {
                CompanyName = companyDto.CompanyName,
                CompanyRegistrationNumber = companyDto.CompanyRegistrationNumber,
                isActive = companyDto.isActive,
                isArchived = companyDto.isArchived,
            };

            user.AppUserCompanySettings.Add(new AppUserCompanySetting
            {
                AppUser = user,
                CompanySettings = company
            });

            _context.Users.Update(user);
            await _context.CompanySettings.AddAsync(company);
            await _context.SaveChangesAsync();
            return new CompanyDto(){
                Id = companyDto.Id,
                CompanyName = companyDto.CompanyName,
                CompanyRegistrationNumber = companyDto.CompanyRegistrationNumber,
                isActive = companyDto.isActive,
                isArchived = companyDto.isArchived,
                UserId = companyDto.UserId
            };
        }

API 调用几乎可以正常工作。它创建公司,正确更新 AppUserCompanySetting table,但 return 数据显示:

{
    "id": 0,
    "companyName": "My Test Company",
    "companyRegistrationNumber": "1985/123456/07",
    "isActive": true,
    "isArchived": false,
    "userId": 2
}

我想弄清楚为什么 API 调用 return 的 ID 为 0 而不是来自 CompanySettings table(或 CompanyDto)的 ID。

我假设您的 companyDto 变量最初包含 0,因为您正在尝试创建一家新公司。如果是这种情况,您实际上永远不会更新代码中的那个字段,因此它保持不变。

你可以return得到这样的结果:

return new CompanyDto()
{
    Id = company.Id,
    CompanyName = company.CompanyName,
    CompanyRegistrationNumber = company.CompanyRegistrationNumber,
    isActive = company.isActive,
    isArchived = company.isArchived,
    UserId = companyDto.UserId
};

基本上,当您映射回 dto 时,您必须使用实际的 company 变量,因为它将设置 Id 属性在调用 SaveChanges.

之后

我认为不要在 return 语句中将 companydto.Id 值分配给 Id 使用 company

return new CompanyDto(){
                Id = company.Id,
                CompanyName = company.CompanyName,
                CompanyRegistrationNumber = company.CompanyRegistrationNumber,
                isActive = company.isActive,
                isArchived = company.isArchived,
                UserId = company.UserId
            };