简体   繁体   中英

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?

See the error here: https://imgur.com/a/2se9eWB

在此处输入图片说明

When I try to affiliated my ProjectResourceRequest with my ProjectResourceAssignment , it writes to the database, but throws that error.

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:

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.

So that problem was that I was returning an EntityFramework core class from the controller. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM