使用 .NET Core MVC 响应显示 属性 之一的 NULL 值

Using .NET Core MVC response displays NULL value of one of property

使用.Net Core 3.1 MVC模式,我一直在开发一个后端系统。有两张表,一张是“TranslationInfo”,一张是“TranslationContent”。 TranslationInfo 有很多 TranslationContent,所以我这样配置了 Dbcontext。

翻译信息

    public class TranslationInfo
    {
        [Key]
        public int Index { get; set; }
        public DateTime InsertedDate { get; set; }
        public DateTime UpdatedDate { get; set; }
    }

翻译内容

   public class TranslationContent
    {
        [Key]
        public int Code { get; set; }
        public string English { get; set; }
        public string French { get; set; }
        public string Status { get; set; }
        public string Message { get; set; }

        [ForeignKey("Index")]
        public TranslationInfo TranslationInfo { get; set; }
    }

除了根据 TranslationInfo.Index 或不根据 TranslationInfo.Index 获取 TranslationContent 列表外,一切正常。结果显示 TranslationContent w/o TranslationInfo 的列表,即使它能够根据 TranslationInfo.Index.

检索结果

'https://localhost:44311/contentsList/3'

的结果
[
    {
        "code": 1,
        "english": "string",
        "french": "string",
        "status": "",
        "message": "",
        "translationInfo": null
    },
    {
        "code": 2,
        "english": "string",
        "french": "string",
        "status": "string",
        "message": "string",
        "translationInfo": null
    },
    {
        "code": 4,
        "english": "cvzxvc",
        "french": "asdfasdfas",
        "status": "Passed",
        "message": null,
        "translationInfo": null
    },
    {
        "code": 10,
        "english": "string",
        "french": "string",
        "status": "string",
        "message": "string",
        "translationInfo": null
    }
]

这里是应用区域。

ITranslationService.cs

  public interface ITranslationServices
    {
        List<TranslationInfo> GetTranslationInfos();

        TranslationInfo GetTranslationInfo(int index);
        TranslationInfo CreateTranslationInfo(TranslationInfo translationInfo);

        TranslationInfo EditTranslationInfo(TranslationInfo translationInfo);
        List<TranslationContent> GetTranslationContents();
        List<TranslationContent> GetTranslationContentsByIndex(int index);

        TranslationContent GetTranslationContent(int code);
        TranslationContent CreateTranslationContent(TranslationContent translationContent);

        TranslationContent EditTranslationContent(TranslationContent translationContent);
    }

TranslationService.cs

using Excel.DB;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Excel.Core
{
    public class TranslationService : ITranslationServices
    {
        private readonly AppDbContext _context;

        public TranslationService(AppDbContext context)
        {
            _context = context;
        }



        /*     Translation Info Area */
        public List<TranslationInfo> GetTranslationInfos()
        {
            return _context.TranslationInfos.ToList();
        }
        public TranslationInfo GetTranslationInfo(int index)
        {
            return _context.TranslationInfos.First(t => t.Index == index);
        }

        public TranslationInfo CreateTranslationInfo(TranslationInfo translationInfo)
        {
            translationInfo.InsertedDate = DateTime.UtcNow;
            translationInfo.UpdatedDate = DateTime.UtcNow;

            _context.Add(translationInfo);
            _context.SaveChanges();
            return translationInfo;
        }

        public TranslationInfo EditTranslationInfo(TranslationInfo translationInfo)
        {
            var dbTranslationInfo = _context.TranslationInfos.First(t => t.Index == translationInfo.Index);
            // update process will be added.

            dbTranslationInfo.UpdatedDate = DateTime.UtcNow;
            _context.SaveChanges();
            return dbTranslationInfo;

        }


        /*     Translation Content Area */
        public List<TranslationContent> GetTranslationContents()
        {
            return _context.TranslationContents.ToList();
        }
        public List<TranslationContent> GetTranslationContentsByIndex(int index)
        {
            return _context.TranslationContents.Where(t => t.TranslationInfo.Index == index).ToList();
        }

        public TranslationContent GetTranslationContent(int code)
        {
            return (TranslationContent)_context.TranslationContents.First(t => t.Code == code);
        }

        public TranslationContent CreateTranslationContent(TranslationContent translationContent)
        {
            var dbTranslationInfo = _context.TranslationInfos.First(t => t.Index == translationContent.TranslationInfo.Index);

            if (dbTranslationInfo == null)
            {
                throw new Exception("Cannot find translation info");
            }

            translationContent.TranslationInfo = dbTranslationInfo;

            _context.Add(translationContent);
            _context.SaveChanges();
            return (TranslationContent)translationContent;
        }

        public TranslationContent EditTranslationContent(TranslationContent translationContent)
        {
            var dbTranslationContent = _context.TranslationContents.First(t => t.Code == translationContent.Code);
            // update process will be added.

            dbTranslationContent.English = translationContent.English;
            dbTranslationContent.French = translationContent.French;
            dbTranslationContent.Message = translationContent.Message;
            dbTranslationContent.Status = translationContent.Status;
            dbTranslationContent.TranslationInfo = translationContent.TranslationInfo;
            _context.SaveChanges();
            return dbTranslationContent;
        }
    }
}

这里是API(控制器)区域 翻译控制器

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Excel.Core;
using Excel.DB;

namespace Excel.API.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class TranslationController : ControllerBase
    {
        private readonly ITranslationServices _translationServices;

        public TranslationController(ITranslationServices translationServices)
        {
            _translationServices = translationServices;
        }

        [HttpGet("/infos")]
        public IActionResult GetTranslationInfos()
        {
            return Ok(_translationServices.GetTranslationInfos());
        }

        [HttpGet("/info/{index}", Name = "GetTranslationInfo")]
        public IActionResult GetTranslationInfo(int index)
        {
            return Ok(_translationServices.GetTranslationInfo(index));
        }

        [HttpPost("/info")]
        public IActionResult CreateTranslationInfo(TranslationInfo translationInfo)
        {
            var newTranslationInfo = _translationServices.CreateTranslationInfo(translationInfo);
            return CreatedAtRoute("GetTranslationInfo", new { newTranslationInfo.Index }, newTranslationInfo);
        }

        [HttpPut("/info")]
        public IActionResult EditTranslationInfo(TranslationInfo translationInfo)
        {
            return Ok(_translationServices.EditTranslationInfo(translationInfo));
        }

        [HttpGet("/contents",Name = "GetTranslationContents")]
        public IActionResult GetTranslationContents()
        {
            return Ok(_translationServices.GetTranslationContents());
        }

        [HttpGet("/contentsList/{index}", Name = "GetTranslationContentsByIndex")]
        public IActionResult GetTranslationContentsByIndex(int index)
        {
            return Ok(_translationServices.GetTranslationContentsByIndex(index));
        }

        [HttpGet("/content/{code}", Name = "GetTranslationContent")]
        public IActionResult GetTranslationContent(int code)
        {
            return Ok(_translationServices.GetTranslationContent(code));
        }

        [HttpPost("/content")]
        public IActionResult CreateTranslationContent(TranslationContent translationContent)
        {
            var newTranslationContent = _translationServices.CreateTranslationContent(translationContent);
            return CreatedAtRoute("GetTranslationContent", new { newTranslationContent.Code }, newTranslationContent);
        }

        [HttpPut("/content")]
        public IActionResult EditTranslationContent(TranslationContent translationContent)
        {
            return Ok(_translationServices.EditTranslationContent(translationContent));
        }
    }
}

你能告诉我如何在获取 TranslationContent 列表时获取 TranslationInfo 吗?

哦,这似乎很简单:

您的请求中必须包含 TranslationInfo。 TranslationContext 是您要求的 table,因此您正在使用的 EFCore 正在删除所有其他 table。

改变这个:

 public List<TranslationContent> GetTranslationContentsByIndex(int index)
 {
            return _context.TranslationContents.Where(t => t.TranslationInfo.Index == index).ToList();
 }

对此:

 public List<TranslationContent> GetTranslationContentsByIndex(int index)
 {
            return _context.TranslationContents.Where(t => t.TranslationInfo.Index == index).Include(x => x.TranslationInfo).ToList();
 }

或者你可以反过来做:

 public List<TranslationInfo> GetTranslationContentsByIndex(int index)
 {
            return _context.TranslationInfo.Where(t => t.Index == index).Include(x => x.TranslationContent).ToList();
 }