handling circular references when saving(post)

56 views Asked by At

I am stuck with an error i can't figure out. I have a complex model with several circular references. I have tried everything i know to handle them but i am still getting a internal server error (code 500) when i attempt saving.

Below are the models and controllers:

public partial class Event 
{
    public Event()
    {
        Recurrences = new HashSet<Recurrence>();
    }

    public int Id { get; set; }

    [Required]
    [StringLength(150)]
    public string Title { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime UpdateDate { get; set; }

    [StringLength(128)]
    public string CreatedBy { get; set; }

    [StringLength(128)]
    public string UpdatedBy { get; set; }

    public ICollection<Recurrence> Recurrences { get; set; }
}


public partial class Recurrence
{
    public Recurrence()
    {
        AspNetUsers = new HashSet<AspNetUser>();
    }

    public int Id { get; set; }

    public int EventId { get; set; }

    [Column(TypeName = "date")]
    public DateTime StartDate { get; set; }

    [Column(TypeName = "date")]
    public DateTime? EndDate { get; set; }

    public bool? AllDay { get; set; }

    public TimeSpan? StartTime { get; set; }

    public TimeSpan? EndTime { get; set; }

    [StringLength(500)]
    public string Venue { get; set; }

    public double? Longitude { get; set; }

    public double? Latitude { get; set; }

    public int? RecurrenceInterval { get; set; }

    public bool? ExcludeWeekends { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime UpdateDate { get; set; }

    [StringLength(128)]
    public string CreatedBy { get; set; }

    [StringLength(128)]
    public string UpdatedBy { get; set; }

    public Event Event { get; set; }

    public RecurrenceType RecurrenceType { get; set; }

    public ICollection<AspNetUser> AspNetUsers { get; set; }
}

public partial class AspNetUser
{
    public AspNetUser()
    {
        Recurrences = new HashSet<Recurrence>();
    }

    public string Id { get; set; }

    [StringLength(256)]
    public string Email { get; set; }

    public bool EmailConfirmed { get; set; }

    public string PasswordHash { get; set; }

    public string SecurityStamp { get; set; }

    public string PhoneNumber { get; set; }

    public bool PhoneNumberConfirmed { get; set; }

    public bool TwoFactorEnabled { get; set; }

    public DateTime? LockoutEndDateUtc { get; set; }

    public bool LockoutEnabled { get; set; }

    public int AccessFailedCount { get; set; }

    [Required]
    [StringLength(256)]
    public string UserName { get; set; }

    public ICollection<Recurrence> Recurrences { get; set; }
}

public class EventDTO
{
    public int Id { get; set; }

    [Required]
    [StringLength(150)]
    public string Title { get; set; }

    public int EventTypeId { get; set; }

    [Column(TypeName = "date")]
    public DateTime StartDate { get; set; }

    [Column(TypeName = "date")]
    public DateTime EndDate { get; set; }

    public bool? AllDay { get; set; }

    public TimeSpan? StartTime { get; set; }

    public TimeSpan? EndTime { get; set; }

    [StringLength(500)]
    public string Venue { get; set; }

    public double? Longitude { get; set; }

    public double? Latitude { get; set; }

    public int RecurrenceTypeId { get; set; }

    public int? RecurrenceInterval { get; set; }

    public bool? ExcludeWeekends { get; set; }

    public DateTime CreateDate { get; set; }

    public DateTime UpdateDate { get; set; }

    [StringLength(128)]
    public string CreatedBy { get; set; }

    [StringLength(128)]
    public string UpdatedBy { get; set; }

    public List<string> UserId { get; set; }
}


public async Task<IHttpActionResult> PostEvent(EventDTO @event)
{
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        Event newEvent = new Event();
        newEvent.Title = @event.Title;
        newEvent.EventTypeId = @event.EventTypeId;
        newEvent.CreateDate = @event.CreateDate;
        newEvent.UpdateDate = @event.UpdateDate;
        newEvent.CreatedBy = @event.CreatedBy;
        newEvent.UpdatedBy = @event.CreatedBy;

        if (newEvent == null) {
            throw new HttpResponseException(
               Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Error creating Event"));
        }

        Recurrence recurrence = new Recurrence();
        recurrence.StartDate = @event.StartDate;
        recurrence.EndDate = @event.EndDate;
        recurrence.AllDay = @event.AllDay;
        recurrence.StartTime = @event.StartTime;
        recurrence.EndTime = @event.EndTime;
        recurrence.Venue = @event.Venue;
        recurrence.Longitude = @event.Longitude;
        recurrence.Latitude = @event.Latitude;
        recurrence.RecurrenceTypeId = @event.RecurrenceTypeId;
        recurrence.RecurrenceInterval = @event.RecurrenceInterval;
        recurrence.ExcludeWeekends = @event.ExcludeWeekends;
        recurrence.CreateDate = @event.CreateDate;
        recurrence.UpdateDate = @event.UpdateDate;
        recurrence.CreatedBy = @event.CreatedBy;
        recurrence.UpdatedBy = @event.CreatedBy;

        if (recurrence == null)
        {
            throw new HttpResponseException(
                    Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Error creating recurrence"));
        }

        var users = db.AspNetUsers.Where(u => @event.UserId.Contains(u.Id));
        foreach (var u in users)
            recurrence.AspNetUsers.Add(u);


        newEvent.Recurrences.Add(recurrence);

        db.Events.Add(newEvent);
        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { id = @event.Id }, newEvent);
    }

When i call the post method i get an internal error code 500 and an error message of "{$id=1, Message=An error has occurred}".

0

There are 0 answers