我有两张桌子:Users
& Profiles
。用户有一个配置文件(1:1),配置文件可能会受到许多用户的影响,每个配置文件都有许多模块,每个模块都有很多操作。
我正在使用直接服务调用将此对象从asmx发送到aspx页面。
由于延迟加载,我收到错误...所以我禁用了延迟加载。
this.Configuration.LazyLoadingEnabled = false;
这很好用,我得到了我的用户,配置文件为null。
要构建菜单树,我必须检索配置文件。我把它包括在内:
User user = new User();
using (cduContext db = new cduContext())
{
// get the user
string encryptedPassword = Encryption.Encrypt(password);
user = (from u in db.Users
where u.UserName.Equals(login) &&
u.Password.Equals(encryptedPassword)
select u).FirstOrDefault();
// Include the users profile
user = db.Users.Include("Profile").FirstOrDefault();
}
return user;
我在javascript调用函数中遇到了这个错误:
在序列化“CDU.Entities.Models.User”类型的对象时检测到循环引用。
当我快速监视用户对象时,在asmx中(发送之前),我发现,该配置文件已包含具有此pofile的用户列表,每个用户都已加载其配置文件...等
请问好吗?
答案 0 :(得分:3)
注意,您的代码应如下所示:
using (cduContext db = new cduContext())
{
// get the user
string encryptedPassword = Encryption.Encrypt(password);
var user = from u in db.Users
where u.UserName.Equals(login) &&
u.Password.Equals(encryptedPassword)
select u;
// Include the users profile
return user.Include("Profile").FirstOrDefault();
}
在你的代码中,你通过用第二个覆盖它来丢弃第一个查询。并且没有合理的理由来创建空白用户。
要解决您的问题,您将决定不想序列化的内容。在您的情况下,您可能不希望序列化Profile.Users
您没有提到您正在使用的序列化程序。我假设您正在使用DataContract序列化程序?
编辑:
您可以使用[IgnoreDataMember]属性标记您的Profile.Users对象。