【问题标题】:InvalidOperationException using Datacontext使用 Datacontext 的 InvalidOperationException
【发布时间】:2012-03-07 09:44:57
【问题描述】:

我在运行此程序时收到 InvalidOperationException(它显示“无法确定属性名称”)。我已经检查了网络,但没有找到解决方案。它出现在 foreach (var c in contacts) 行。

DataContext ctx = new DataContext("CrmConnection");

        var contacts = from c in ctx.contacts
                       where c != null
                       select new
                       {
                           acct = c.parentcustomerid == null ? "" : c.parentcustomerid.name,
                           last = c.lastname == null ? "" : c.lastname,
                           first = c.firstname == null ? "" : c.firstname
                       };

        List<string> lines = new List<string>();

        try
        {
            foreach (var c in contacts) *ex*
            {
                Console.WriteLine(c.acct);
                Console.ReadLine();
                lines.Add(string.Format("{0}\t{1}\t{2}", c.acct, c.last, c.first));
                Console.WriteLine(c.acct);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(String.Format("Error: {0}", ex));
        }

如果您有任何想法,请告诉我。谢谢。

【问题讨论】:

  • 这是您收到的确切错误消息,还是还有更多?
  • ex 是我添加的。它实际上并没有运行。这是完整的错误。 Error: System.InvalidOperationException: Cannot determine the attribute name. at Microsoft.Xrm.Client.Linq.CrmQueryProvider.TranslateExpressionToAttributeN ame(Expression exp) at Microsoft.Xrm.Client.Linq.CrmQueryProvider.AddConditionForBinary(QueryExpr ession qe, IDictionary 2 filters, BinaryExpression exp, Expression parent, Boole an negate) at Microsoft.Xrm.Client.Linq.CrmQueryProvider.&lt;&gt;c__DisplayClass2f.&lt;TranslateW here&gt;b__2e(Expression exp, Expression parent)(由于字数限制,其余被截断)

标签: c# dynamics-crm datacontext crm invalidoperationexception


【解决方案1】:
where c != null

是问题。你在哪个字段检查空c.parentcustomerid !=null?你不能对实体做空检查。

此外,ID 之外的 name 属性将适用于使用 LINQ to Crm 进行的查找

【讨论】:

    【解决方案2】:

    我怀疑 c.parentcustomerid 是相关对象的 id。它没有您尝试访问的名为 name 的属性。

    如果您想要获取父客户的名称,您想要访问“parentcustomerid”所指向的实际客户对象。

    如果您想要的是帐户 ID(根据 acct 属性猜测),那么只需更改

    acct = c.parentcustomerid == null ? "" : c.parentcustomerid.name
    

    acct = c.parentcustomerid == null ? "" : c.parentcustomerid
    

    【讨论】:

    • parentcustomerid 似乎不是问题所在。当我用 c.address1_city 替换它时(我知道过去曾为 datacontext 工作过),我得到了同样的错误。
    • 您能发布您的数据库和/或映射代码的实际架构吗?正如 Reza 所提到的,异常发生在 foreach 中,因为在您对其进行迭代之前不会执行查询...实际问题出在您的查询中(因此,从 C# 类到数据库的映射可能不正确)跨度>
    【解决方案3】:

    执行查询时发生此异常,当迭代联系人上下文时尝试从 sql 执行查询。我认为发生此错误是因为您的上下文模型未正确映射到您的数据库,可能是不同的表名或属性。

    【讨论】:

    • 我认为你可能是对的......这真的很奇怪,因为我使用的是在代码中用于不同功能的相同连接字符串 (DataContext ctx = new DataContext("CrmConnection");),并且那个有效..
    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2012-05-02
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    相关资源
    最近更新 更多