1

我得到一个代码片段的“ObjectContext 实例已被处置,不能再用于需要连接错误的操作” - 在第二个查询(DataBind)上。它在开发中运行良好。开发使用实体框架版本 6 - 服务器可能是版本 4。这是代码:

    using (MainModelContainer m = new MainModelContainer())
    {
        AlltheNames = m.PlayerStatsNames.OrderBy(x => x.FieldName).ToArray();
        HeadingNames = new SortedDictionary<string, string>();
        for (i=0; i < AlltheNames.Length; i++)
        {
            HeadingNames.Add(AlltheNames[i].FieldName, AlltheNames[i].ShortHeader);
        }

        m.Connection.Open();
        var FieldNames = new ObjectQuery<DbDataRecord>(theQuery, m).ToArray();
        foreach(DbDataRecord FieldName in FieldNames)
        {
            if ((bool)FieldName[1])
            {
                sb.Append(",ps.");
                sb.Append(FieldName[0].ToString());
            }
        }
        sb.Append(" FROM PlayerStats AS ps ");
        sb.Append("LEFT OUTER JOIN RealTeams AS rt ON ps.RealTeamId=rt.RealTeamId WHERE ps.Player.PlayerId=");
        sb.Append(PlayerID);
        var PlayerStatistics = m.CreateQuery<DbDataRecord>(sb.ToString());
        rgStats.DataSource = PlayerStatistics;
        try
        {
            rgStats.DataBind();
        }
        catch (Exception ex)
        {

        }
    }
4

0 回答 0