简体   繁体   English

MVC3 & Entity Framework 4 中的循环引用错误

[英]Cyclical reference error in MVC3 & Entity Framework 4

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.我承认使用 MVC3/EF4 轻而易举,并且上下文:模型比我希望的工作得更好(尽管我总是对使事情发生在幕后的构造/框架感到厌烦,但是嘿,我是老派的灰胡子),一切都很顺利,直到我遇到这个问题。
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.每个表(实体)都有一个employeeID 字段,并且对于每个表中的大多数记录,它可以(并且通常是)不同的员工。 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".显然,DbContext 中只有一个表可以定义一个“虚拟员工员工”(或我选择的任何名称),否则我会得到可怕的“循环引用错误”。 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.我可能必须重写 OnModelCreating() ,但那(流利的 API)完全超出了我的范围。 Any EF gurus out there????有EF大师吗????

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

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 模型中抽象出来,但是如果您想坚持使用这些模型,请使用此答案中的方法来摆脱您的周期性参考问题:

EF 4.1 - Code First - JSON Circular Reference Serialization Error EF 4.1 - 代码优先 - JSON 循环参考序列化错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM