Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core mvc 使用.NET核心MVC响应显示其中一个属性的空值_Asp.net Core Mvc_Asp.net Core Webapi_Asp.net Core 3.1 - Fatal编程技术网

Asp.net core mvc 使用.NET核心MVC响应显示其中一个属性的空值

Asp.net core mvc 使用.NET核心MVC响应显示其中一个属性的空值,asp.net-core-mvc,asp.net-core-webapi,asp.net-core-3.1,Asp.net Core Mvc,Asp.net Core Webapi,Asp.net Core 3.1,使用.NETCore3.1MVC模式,我一直在开发一个后端系统。有两个表,一个是“TranslationInfo”,另一个是“TranslationContent”。TranslationInfo有很多TranslationContent,所以我像这样配置了Dbcontext 翻译信息 public class TranslationInfo { [Key] public int Index { get; set; } public

使用.NETCore3.1MVC模式,我一直在开发一个后端系统。有两个表,一个是“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是否获取TranslationContent列表外,一切正常。结果显示不带TranslationInfo的TranslationContent列表,即使它允许基于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);
    }
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;
        }
    }
}


在获取翻译内容列表时,您能告诉我如何获取翻译信息吗?

哦,这似乎很简单:

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

更改此项:

 public List<TranslationContent> GetTranslationContentsByIndex(int index)
 {
            return _context.TranslationContents.Where(t => t.TranslationInfo.Index == index).ToList();
 }
public List GetTranslationContentsByIndex(int-index)
{
返回_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 GetTranslationContentsByIndex(int-index)
{
返回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();
 }
public List GetTranslationContentsByIndex(int-index)
{
返回_context.TranslationInfo.Where(t=>t.Index==Index).Include(x=>x.TranslationContent.ToList();
}

这很常见,你必须关心。一种简单的方法是使用该属性来不序列化第二个深度路径,或者在JSON配置中获得一个全局设置,使自引用不应被完全填充,或者使用Automapper忽略循环。这种情况如何?正如您所提到的,当我调用'GetTranslationInfo()'时,我希望显示响应,结果包括每个TranslationInfo上的TranslationContents。所以我更新了如下“return _context.translationfos.Include(x=>x.TranslationContents.ToList();”,但无法获取它,错误为“System.Text.Json.JsonException:检测到不支持的可能的对象循环”。这可能是由于一个循环造成的,或者如果对象深度大于允许的最大深度32,那么正如我所说的,在每个相互引用的表上都会出现这个问题。这在EFCore中是众所周知的,这里有3种可能的解决方案:1)在JSON配置中禁用引用globaly一次2)使用Automapper并在映射时禁用每个数据模型3)使用一个告诉JSON转换器不要渲染它的属性。这里有一个链接可以帮助您:您是对的!将“Microsoft.AspNetCore.Mvc.NewtonsoftJson”应用于API后,它会正确显示结果。下次我会尝试使用Automapper,而不是您提到的这个软件包。谢谢!!!
 public List<TranslationInfo> GetTranslationContentsByIndex(int index)
 {
            return _context.TranslationInfo.Where(t => t.Index == index).Include(x => x.TranslationContent).ToList();
 }