简体   繁体   English

ASP.NET Core Web API & Entity Framework Core; 为什么我收到这个错误?

[英]ASP.NET Core Web API & Entity Framework Core; why am I getting this error?

I understand that the serializer in ASP.NET Core may not be able to handle certain responses, but why do I get this error when I don't try to return the object in question?我知道 ASP.NET Core 中的序列化程序可能无法处理某些响应,但是当我不尝试返回相关对象时为什么会出现此错误?

See the error here: https://imgur.com/a/2se9eWB在此处查看错误: https : //imgur.com/a/2se9eWB

在此处输入图片说明

When I try to affiliated my ProjectResourceRequest with my ProjectResourceAssignment , it writes to the database, but throws that error.当我尝试将我的ProjectResourceRequest与我的ProjectResourceAssignment关联时,它会写入数据库,但会引发该错误。

Model classes:模型类:

public abstract class DivDbBase
{
    [Key]
    public int Id { get; set; }

    [Required]
    public DateTimeOffset DateCreated { get; set; }

    public int CreatedByUserId { get; set; }

    [Required]
    public DateTimeOffset LastDateModified { get; set; }

    public int LastModifiedByUserId { get; set; }

    //[ForeignKey("CreatedByUserId")]
    //public DivDbUser CreatedByUser { get; set; }

    //[ForeignKey("LastModifiedByUserId")]
    //public DivDbUser LastModifiedByUser { get; set; }
}

public interface IDivDbEvent
{
    [Required]
    public DateTimeOffset StartDate { get; set; }

    [Required]
    public DateTimeOffset EndDate { get; set; }
}

public class DivDbProjectResourceRequest : DivDbBase, IDivDbEvent
{
    public int ProjectId { get; set; }
    public int RoleId { get; set; }

    [Required]
    public DateTimeOffset StartDate { get; set; }

    [Required]
    public DateTimeOffset EndDate { get; set; }

    [ForeignKey("ProjectId")]
    public DivDbProject Project { get; set; }

    [ForeignKey("RoleId")]
    public DivDbRole Role { get; set; }
}
    public class DivDbProjectResourceAssignment : DivDbBase, IDivDbEvent
{
    public int ProjectId { get; set; }
    public int RoleId { get; set; }
    public int UserId { get; set; }

    public int? ProjectResourceRequestId { get; set; }

    public DateTimeOffset StartDate { get; set; }
    public DateTimeOffset EndDate { get; set; }

    [ForeignKey("ProjectId")]
    public DivDbProject Project { get; set; }

    [ForeignKey("RoleId")]
    public DivDbRole Role { get; set; }

    [ForeignKey("UserId")]
    public DivDbUser User { get; set; }

    [ForeignKey("ProjectResourceRequestId")]
    public DivDbProjectResourceRequest ProjectResourceRequest { get; set; }
}

Then in my controller ProjectResourceRequest already exists, and I'm not trying to return either of the complex objects:然后在我的控制器中ProjectResourceRequest已经存在,我不想返回任何一个复杂对象:

DivDbProjectResourceAssignment dbProjectResourceAssignment = new DivDbProjectResourceAssignment()
{
    ProjectId = 1,
    RoleId = 1,
    UserId = 1,
    StartDate = DateTimeOffset.Now,
    EndDate = DateTimeOffset.Now,
    ProjectResourceRequestId = 1,
    CreatedByUserId = 1,
    LastModifiedByUserId = 1,
    LastDateModified = DateTimeOffset.Now,
    DateCreated = DateTimeOffset.Now
};
mContext.ProjectResourceAssignments.Add(dbProjectResourceAssignment);

mContext.SaveChanges();

Items are added correctly, but it crashes the controller response regardless of if I return that item.项目已正确添加,但无论我是否返回该项目,它都会使控制器响应崩溃。 If I am to get to the item from my database using mContext now, I get the same error...once again whether or not I return that item in any form from the API controller.如果我现在要使用mContext从我的数据库中获取该项目,我会得到同样的错误......无论我是否从 API 控制器以任何形式返回该项目。

So that problem was that I was returning an EntityFramework core class from the controller.所以这个问题是我从控制器返回了一个 EntityFramework 核心类。 The reason this surprised me, is that the object being returned did not have any relationships, but just the mere fact that other objects that mContext interacted with did, caused the entire response to error.这让我感到惊讶的原因是,被返回的对象没有任何关系,而仅仅是与 mContext 交互的其他对象确实如此,就导致了整个响应错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 Web API 使用实体框架核心 - ASP.NET Core Web API using Entity Framework Core 如何使用实体框架通过 ASP.NET Core Web API 处理通用查询? - How can I process a generic query through ASP.NET Core Web API using Entity Framework? ASP.NET Core 3.1 Web API 和 Entity Framework Core:AutoMapper 获取错误映射类型 MIssing Type mpa 配置或不支持的映射 - ASP.NET Core 3.1 Web API and Entity Framework Core : AutoMapper getting error mapping types MIssing Type mpa configuration or unsupported mapping ASP.NET 核心 Web API 与实体框架核心默认脚手架 Z594C103F2C6E04C3DAZ8AB05E 混淆 put? - ASP.NET Core Web API with Entity Framework Core default scaffolded controller confuses put and post? 用谷歌授权 asp.net 核心 web api(实体框架核心) Z7C82E852FB415F250Z92 - authorize asp.net core web api (entity framework core) with google oauth 为什么我在 ASP.NET Core 6 C# 中收到此错误? - Why am I getting this error in ASP.NET Core 6 C#? ASP.NET Core Web API 和 Entity Framework 同时检查实体是否存在于数据库中 - ASP.NET Core Web API and Entity Framework simultaneously check if entity exists in database 具有多个关键实体的Asp.net核心Entity Framework 6错误 - Asp.net core Entity Framework 6 error with multiple key entity ASP.NET Core MVC 1.3 Web API + Entity Framework,我无法按名称搜索数据库,但可以按 ID - ASP.NET Core MVC 1.3 Web API + Entity Framework, I can't search a database by name, but I can by id 我在 ASP.NET Core Web API 应用程序上配置 CORS 时出现 CORS 错误 - I'm getting CORS error with CORS configured on ASP.NET Core Web API app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM