0

I admit using MVC3/EF4 has been a breeze and the context:models work better then I'd hoped (though I'm always leary of constructs/frameworks that make things happen behind some curtain, but hey I'm old school grey beard), and all went well until I hit this issue.
I've seen many postings related to this issue but don't know how to solve it in my case. Each table(entity) has an employeeID field and it can (and usually is) a different employee for most records in each table. Apparently only one table in the DbContext can have a 'virtual employee employee' (or whatever I choose to name it) defined or else I get the dreaded "cyclical reference error". Ideally I'd like all three tables to have it so I can easily access the employees name. I may have to override the OnModelCreating() but that's (fluent API) totally out of my league. Any EF gurus out there????

Models:

class meeting{
int meetingID; //key
string description  {get;set;}
int employeeID {get;set;}   // who scheduled
public virtual employee employee  {get;set;}
public virtual ICollection<agenda> agendas {get;set;}
}

class agenda{
int agendaID {get;set;}   // key
int employeeID  {get;set;}  // initiator
public virtual employee employee {get;set;}
public virtual ICollection<actionItem> actionItems {get;set;}
}

class actionItem{
int actioItemID {get;set;} //key
string description {get;set;}
int employeeID {get;set;} // action item lead
public virtual employee employee {get;set;}
}

class employee{
int employeeID  {get;set;}//key
string name {get;set;}
}

context:
public class meetings:DbContext{
public DbSet<meeting> meetings {get;set;}
public DbSet<agenda> Agendas
public DbSet<actionItem> actionItems{get;set;}
}
Gio
  • 4,099
  • 3
  • 30
  • 32

1 Answers1

3

I assume you're getting this error on serialization of your models. Most people use a second set of models to abstract from the EF models, but if you want to stick with those, use the approach from this answer to get rid of your cyclical reference problem:

EF 4.1 - Code First - JSON Circular Reference Serialization Error

Community
  • 1
  • 1
Milimetric
  • 13,411
  • 4
  • 44
  • 56
  • Actually turning off cascading deletes got rid of the error, but I guess I'll have to wait until I get to the issue of deletes to see what effect that has. – Gio Jul 19 '11 at 14:54