I have read a lot of previous solutions for this problem but none worked for me.
I have a circular relation between Event and User object:
public class Event : EntityData
{
[Required]
[ForeignKey("Creator")]
public string CreatorId { get; set; }
public User Creator { get; set; }
[InverseProperty("ParticipantIn")]
public virtual ICollection<User> Participants { get; set; }
[InverseProperty("ManagerIn")]
public virtual ICollection<User> Managers { get; set; }
}
As you can see I have three references to User from this class: event creator, list of managers, and list of participants.
The user class contains references to Event as well:
public class User: EntityData
{
[InverseProperty("Participants")]
public virtual ICollection<Event> ParticipantIn { get; set; }
[InverseProperty("Managers")]
public virtual ICollection<Event> ManagerIn { get; set; }
}
Now, the issue is that when I try to serialize an event, like in my createEvent function, it tells me that there is a self referencing loop, that is because when the event is created, I am adding it to the creator's 'ManagerIn' Collection.
That line causes Event->Creator->ManagerIn->Event->Creator->..... LOOP
I tried anything, I also had a version of this code with public virtual User Creator, in order to make it load lazily..
For now, my solution is very not elegant, before returning the event to the client I am performing:
event.Creator = null;
and
event.Managers = null;
in order to avoid a self referencing loop.
What is the right way to solve this kind of problem?
Thanks in advance, Liran!