Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存时处理循环引用(post)_C#_Asp.net_Asp.net Web Api_Circular Reference - Fatal编程技术网

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

C# 保存时处理循环引用(post),c#,asp.net,asp.net-web-api,circular-reference,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; }

我被一个我无法理解的错误缠住了。我有一个复杂的模型,有几个循环引用。我已经尝试了所有我知道的方法来处理它们,但是当我尝试保存时,仍然会遇到一个内部服务器错误(代码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);
    }
公共部分类事件
{
公共活动()
{
Recurrences=新HashSet();
}
公共int Id{get;set;}
[必需]
[长度(150)]
公共字符串标题{get;set;}
公共日期时间CreateDate{get;set;}
公共日期时间更新日期{get;set;}
[第128段]
通过{get;set;}创建的公共字符串
[第128段]
由{get;set;}更新的公共字符串
公共ICollection重复出现{get;set;}
}
公共部分类递归
{
公众复发()
{
AspNetUsers=newhashset();
}
公共int Id{get;set;}
public int EventId{get;set;}
[列(TypeName=“日期”)]
公共日期时间起始日期{get;set;}
[列(TypeName=“日期”)]
公共日期时间?结束日期{get;set;}
公共布尔?全天{get;set;}
公共时间跨度?开始时间{get;set;}
公共时间跨度?结束时间{get;set;}
[长度(500)]
公共字符串地址{get;set;}
公共双经度{get;set;}
公共双纬度{get;set;}
公共整数?重复间隔{get;set;}
公共布尔?排除周末{get;set;}
公共日期时间CreateDate{get;set;}
公共日期时间更新日期{get;set;}
[第128段]
通过{get;set;}创建的公共字符串
[第128段]
由{get;set;}更新的公共字符串
公共事件事件{get;set;}
公共RecurrenceType RecurrenceType{get;set;}
公共ICollection AspNetUsers{get;set;}
}
公共部分类AspNetUser
{
公共AspNetUser()
{
Recurrences=新HashSet();
}
公共字符串Id{get;set;}
[StringLength(256)]
公共字符串电子邮件{get;set;}
公共布尔值{get;set;}
公共字符串密码哈希{get;set;}
公共字符串SecurityStamp{get;set;}
公共字符串PhoneNumber{get;set;}
public bool phonenumberconfirm{get;set;}
公共bool TwoFactorEnabled{get;set;}
公共日期时间?LockoutEndDateUtc{get;set;}
public bool LockoutEnabled{get;set;}
public int AccessFailedCount{get;set;}
[必需]
[StringLength(256)]
公共字符串用户名{get;set;}
公共ICollection重复出现{get;set;}
}
公共类事件
{
公共int Id{get;set;}
[必需]
[长度(150)]
公共字符串标题{get;set;}
public int EventTypeId{get;set;}
[列(TypeName=“日期”)]
公共日期时间起始日期{get;set;}
[列(TypeName=“日期”)]
公共日期时间结束日期{get;set;}
公共布尔?全天{get;set;}
公共时间跨度?开始时间{get;set;}
公共时间跨度?结束时间{get;set;}
[长度(500)]
公共字符串地址{get;set;}
公共双经度{get;set;}
公共双纬度{get;set;}
public int RecurrenceTypeId{get;set;}
公共整数?重复间隔{get;set;}
公共布尔?排除周末{get;set;}
公共日期时间CreateDate{get;set;}
公共日期时间更新日期{get;set;}
[第128段]
通过{get;set;}创建的公共字符串
[第128段]
由{get;set;}更新的公共字符串
公共列表用户标识{get;set;}
}
公共异步任务PostEvent(EventDTO@event)
{
如果(!ModelState.IsValid)
{
返回请求(ModelState);
}
Event newEvent=新事件();
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){
抛出新的HttpResponseException(
Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,“错误创建事件”);
}
递归=新的递归();
recurrence.StartDate=@event.StartDate;
recurrence.EndDate=@event.EndDate;
recurrence.AllDay=@event.AllDay;
recurrence.StartTime=@event.StartTime;
recurrence.EndTime=@event.EndTime;
recurrence.vention=@event.vention;
recurrence.Longitude=@event.Longitude;
returnal.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(递归==null)
{
抛出新的HttpResponseException(
Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,“创建重复周期时出错”);
}
var users=db.AspNetUsers.Where(u=>@event.UserId.Contains(u.Id));
foreach(用户中的var u)
递归.AspNetUsers.Add(u);
newEvent.Recurrences.Add(Recurrences);
db.Events.Add(newEvent);
等待db.saveChangesSync();
返回CreatedAtRoute(“DefaultApi”,new{id=@event.id},newEvent);
}

当我调用post方法时,我得到一个内部错误代码500和一条错误消息“{$id=1,message=a error has accurrent}”。

这是很多代码。您能指出错误/异常发生的位置吗?你运行这个程序时有没有附加调试器?他说了什么,我们真的不需要所有的空行。错误正在发生