我在使用以下代码时遇到了以上错误。
我在某处读到我说应该删除所有对Microsoft.Crm.Sdk.dll
的引用,但这没用。如果可以的话,请协助。
using System;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using InvoiceMVCApp.Models;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Client;
namespace InvoiceMVCApp.DAL
{
public class DAL_InvoicesEntity
{
public List<InvoicesModel> RetriveRecords()
{
var connection = new CrmConnection("Server=CRM Online");
using (OrganizationService service = new OrganizationService("ConnectionString", connection))
{
QueryExpression query = new QueryExpression
{
EntityName = "invoice",
ColumnSet = new ColumnSet("invoiceid", "customer", "invoicename", "paymentreceived", "commission", "adminfee", "discountamount")
};
List<InvoicesModel> info = new List<InvoicesModel>();
EntityCollection invoiceRecord = service.RetrieveMultiple(query);
if (invoiceRecord != null && invoiceRecord.Entities.Count > 0)
{
InvoicesModel invoiceModel;
for (int i = 0; i < invoiceRecord.Entities.Count; i++)
{
invoiceModel = new InvoicesModel();
if (invoiceRecord[i].Contains("invoiceid") && invoiceRecord[i]["invoiceid"] != null)
invoiceModel.InvoiceID = (Guid)invoiceRecord[i]["invoiceid"];
if (invoiceRecord[i].Contains("customer") && invoiceRecord[i]["customer"] != null)
invoiceModel.ClientName = ((EntityReference)invoiceRecord[i]["customer"]);
if (invoiceRecord[i].Contains("invoiceid") && invoiceRecord[i]["invoiceid"] != null)
invoiceModel.InvoiceNumber = invoiceRecord[i]["invoiceid"].ToString();
if (invoiceRecord[i].Contains("commission") && invoiceRecord[i]["commission"] != null)
invoiceModel.Commission = (decimal)(invoiceRecord[i]["commission"]);
if (invoiceRecord[i].Contains("adminfee") && invoiceRecord[i]["adminfee"] != null)
invoiceModel.AdminFee = (decimal)invoiceRecord[i]["adminfee"];
if (invoiceRecord[i].Contains("discountamount") && invoiceRecord[i]["discountamount"] != null)
invoiceModel.Discount = ((decimal)invoiceRecord[i]["discountamount"]);
info.Add(invoiceModel);
}
}
return info;
}
}
}
}
答案 0 :(得分:0)
尝试清洁->重建。不必删除引用,您可以使用全名来代替,也称为Microsoft.Xrm.Sdk.Query.QueryExpression query = new Microsoft.Xrm.Sdk.Query.QueryExpression(){};
答案 1 :(得分:0)
您的列集中似乎有无效的列名。
"customer"
应该是"customerid"
。
"invoicename"
应该是"name"
。
像"adminfee"
,"commission"
和"paymentreceived"
这样的自定义字段将始终根据创建时所用的解决方案具有前缀(默认为new_
)。
此外,当前通过SDK进行连接的首选方法是Microsoft.Xrm.Tooling.Connector命名空间中的CrmServiceClient类。
要找到它,您可以在NuGet中搜索“ Xrm Tooling”。
此外,如果要将结果转换为自定义InvoicesModel类,则可能需要研究Early Bound编程风格,在其中使用工具自动生成包含您的属性的代理类D365实体。这样就不必为从系统中检索的数据创建自己的“模型”类。
这是一个可行的示例,可为您提供一些可能性的想法:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Linq;
public class InvoicesModel
{
public Guid? InvoiceID { get; set; }
public EntityReference ClientName { get; set; }
public string InvoiceNumber { get; set; }
public string Name { get; set; }
public bool? PaymentReceived { get; set; }
public decimal? Commission { get; set; }
public decimal? AdminFee { get; set; }
public decimal? DiscountAmount { get; set; }
}
public List<InvoicesModel> RetrieveInvoiceModels(string connectionString)
{
var svc = new CrmServiceClient(connectionString);
var query = new QueryExpression
{
EntityName = "invoice",
ColumnSet = new ColumnSet("invoiceid", "customerid", "invoicenumber", "name", "discountamount"),
TopCount = 10
///Invalid column names: "paymentreceived", "commission", "adminfee",
};
var invoices = svc.RetrieveMultiple(query).Entities.ToList();
var invoiceModels = invoices.Select(i =>
new InvoicesModel
{
InvoiceID = i.GetAttributeValue<Guid>("invoiceid"),
ClientName = i.GetAttributeValue<EntityReference>("customerid"),
InvoiceNumber = i.GetAttributeValue<string>("invoicenumber"),
Name = i.GetAttributeValue<string>("name"),
DiscountAmount = i.GetAttributeValue<Money>("new_discountamount")?.Value
///for when columnn names are fixed:
//PaymentReceived = i.GetAttributeValue<bool>("new_paymentreceived")
//AdminFee = i.GetAttributeValue<Money>("new_adminfee")?.Value,
//Commission = i.GetAttributeValue<Money>("new_commission")?.Value,
})
.ToList();
return invoiceModels;
}