这是我的用户模型或类实体框架 - 选择特定的列
public int UserID { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }
public int CompanyID { get; set; }
public int BranchID { get; set; }
public bool RecordState { get; set; }
public virtual Branch Branch { get; set; }
public virtual Company Company { get; set; }
,这是我公司类
public int CompanyID { get; set; }
public string CompanyName { get; set; }
public string CompanyShortName { get; set; }
public string CompanyAddress { get; set; }
public string CompanyPhone { get; set; }
public string CompanyEmail { get; set; }
public string CompanyFax { get; set; }
public bool RecordState { get; set; }
public virtual List<Branch> Branches { get; set; }
public virtual List<Customer> Customers { get; set; }
public virtual List<Market> Markets { get; set; }
public virtual List<Seller> Sellers { get; set; }
public virtual List<User> Users { get; set; }
这是我的[的WebMethod]
public User getUser(int id)
{
User user = db.Users
.Include(c => c.Company)
.Where(i => i.UserID == id)
.FirstOrDefault<User>();
Company company = db.Companies
.Where(i => i.CompanyID == user.CompanyID)
.FirstOrDefault<Company>();
company.Branches = null;
company.Customers = null;
company.Markets = null;
company.Sellers = null;
company.Branches = null;
company.Users = null;
user.Company = company;
return user;
}
我的方法长期以来我想避免循环引用,但我认为我的步骤不好,它需要很多步骤,我想知道我有任何为什么我C使用一个查询让公司内部的用户公司,它也应该返回一个对象类型的用户,因为?我真的很抱歉我的英文不好
当我刚刚使用第一行时,我得到了这个错误System.InvalidOperationException:生成XML文档时发生错误。 ---> System.InvalidOperationException:在序列化GraduateServer.Models.User类型的对象时检测到循环引用。 – user3367149
您可能需要在EF环境中关闭延迟加载。正如另一条评论所建议的,EF设计用于延迟加载,这意味着相关实体在访问时将被检索。当从Web服务传递对象时,您确切知道您需要的相关实体,因此您应该关闭惰性加载并使用“包含调用”明确要检索的相关记录。创建你的DbContext后,这样做:'context.Configuration.LazyLoadingEnabled = FALSE;' – jmcilhinney
最前一页它的关闭,第二就请写个简单的代码,我想要的是,当我通过ID我想包括企业用户照顾,但我不想得到公司的所有栏目我想选择我想要的,对不起我的英文不好 – user3367149