C# 如果通过导航属性获取的实体框架数据不是空的,则会导致错误
我正在使用WCF服务,包括从现有数据库生成的.edmx文件。在begging时出现了一个问题,即在WCF级别成功获取并发送了数据,但在MVC控制器中出现了连接关闭的错误,据我所知,这是因为配置。ProxyCreationEnabled=true,当我将其更改为false时,一切似乎都正常工作,直到我尝试从链接的多个表中获取数据 因此,.edmx文件中有自动生成的类UserProfile和角色 据我所知,这称为导航属性。我的问题是-当我试图发送收集的数据时,如:C# 如果通过导航属性获取的实体框架数据不是空的,则会导致错误,c#,entity-framework,wcf,linq-to-entities,navigation-properties,C#,Entity Framework,Wcf,Linq To Entities,Navigation Properties,我正在使用WCF服务,包括从现有数据库生成的.edmx文件。在begging时出现了一个问题,即在WCF级别成功获取并发送了数据,但在MVC控制器中出现了连接关闭的错误,据我所知,这是因为配置。ProxyCreationEnabled=true,当我将其更改为false时,一切似乎都正常工作,直到我尝试从链接的多个表中获取数据 因此,.edmx文件中有自动生成的类UserProfile和角色 据我所知,这称为导航属性。我的问题是-当我试图发送收集的数据时,如: List<UserProfi
List<UserProfile> result = Context.UserProfiles
.Include(s => s.webpages_Roles)
.Where(z => z.UserId == id)
.ToList();
我收到与配置相同的错误。ProxyCreationEnabled为true。我还注意到,如果webpages\u Roles集合为空,则一切正常,但如果至少有一条记录,我将收到错误消息。请帮助我,解决它
此外,我还将服务配置为写入异常,消息是
网页\u角色“包含周期,如果禁用引用跟踪,则无法序列化。”
解决方案是用类修饰UserProfile和web页面
[DataContractsReference=true]属性
答案在
public partial class webpages_Roles
{
public webpages_Roles()
{
this.UserProfiles = new HashSet<UserProfile>();
}
public int RoleId { get; set; }
public string RoleName { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
UserId
RoleId
List<UserProfile> result = Context.UserProfiles
.Include(s => s.webpages_Roles)
.Where(z => z.UserId == id)
.ToList();