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

asp.net-core asp.net-web-api c# entity-framework-core

Question

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

enter image description here

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.

1
0
2/1/2020 7:31:40 AM

Popular Answer

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.

0
1/31/2020 9:44:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow