【问题标题】:If Entity Framework data obtained via navigation properties isn't empty, it causes error如果通过导航属性获取的 Entity Framework 数据不为空,则会导致错误
【发布时间】:2014-10-20 22:14:44
【问题描述】:

我有WCF 服务,包含.edmx 文件,从现有数据库生成。在乞讨时出现问题,在WCF 级别数据已成功获取并发送,但在MVC Controller 出现连接已关闭的错误,据我了解这是因为Configuration.ProxyCreationEnabled = true; ,当我将其更改为false 时,一切似乎都很好,直到我尝试从多个链接的表中获取数据。

所以,.edmx 文件,有自动生成的类UserProfileRoles

public partial class UserProfile
{
    public UserProfile()
    {
        this.webpages_Roles = new HashSet<webpages_Roles>();
    }

    public int UserId { get; set; }
    public string UserName { get; set; }

    public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}

还有

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

据我了解,这称为Navigation Property。我的问题是 - 当我试图发送收集的数据时:

List<UserProfile> result = Context.UserProfiles
            .Include(s => s.webpages_Roles)
            .Where(z => z.UserId == id)
            .ToList();

我收到的错误与Configuration.ProxyCreationEnabled 相同。我还注意到,如果webpages_Roles Collection 为空,一切正常,但如果至少有一条记录 - 我会收到错误消息。请帮助我,解决它。

另外,我已经将服务配置为写入异常,并且消息是

webpages_Roles' 包含循环,如果禁用引用跟踪,则无法序列化。'。

【问题讨论】:

    标签: c# entity-framework wcf linq-to-entities navigation-properties


    【解决方案1】:

    解决方案是用 UserProfilewebpages_Roles[DataContract(IsReference = true)]属性

    答案在this arcticle找到

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多