保存时处理循环引用(post)

时间:2015-06-16 22:33:58

标签: c# asp.net asp.net-web-api circular-reference

我遇到了一个我无法弄清楚的错误。我有一个带有几个循环引用的复杂模型。我已经尝试了所有我知道的处理它们,但是当我尝试保存时,我仍然收到内部服务器错误(代码500)。

以下是型号和控制器:

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);
    }

当我调用post方法时,我收到内部错误代码500和错误消息“{$ id = 1,Message =发生错误}”。

0 个答案:

没有答案