CRM 2011开发:如果我尝试获取相关实体,则会收到以下错误:对象引用未设置为对象的实例

时间:2013-06-08 22:37:28

标签: dynamics-crm-2011 dynamics-crm

我尝试获取一个相关实体并得到此错误:对象引用未设置为对象的实例。

见下面我的代码:

public IEnumerable<RUBAnnotation> GetAnnotationsByServiceRequestId(string serviceRequestId)
        {
            List<Annotation> annotationList = new List<Annotation>();

            try
            {
                using (OrganizationServiceProxy organizationProxy = new OrganizationServiceProxy(organisationWebServiceUri, null, userCredentials, deviceCredentials))
                {
                    organizationProxy.EnableProxyTypes();

                    var service = (IOrganizationService)organizationProxy;
                    OrganizationServiceContext orgContext = new OrganizationServiceContext(service);

                    Relationship rel = new Relationship("Incident_Annotation");

                    // get all incidents by incidentId
                    IEnumerable<Incident> incidents = from c in orgContext.CreateQuery<Incident>()
                                         where c.Id == new Guid(serviceRequestId)
                                         select c;

                    return GetAnnotationsEntities(incidents);
                }

            }
            catch (Exception)
            {
                throw;
            }

            return null;
        }

private List<RUBAnnotation> GetAnnotationsEntities(IEnumerable<Incident> incidents)
        {
            List<RUBAnnotation> annotationsList = new List<RUBAnnotation>();

            try
            {

                    foreach (Incident incident in incidents)
                    {
                        foreach (var annotation in incident.Incident_Annotation) // HERE OCCURS THE EXCEPTION!!
                        {
                            if (!string.IsNullOrEmpty(annotation.NoteText))
                            {
                                var customAnnotation = new RUBAnnotation();

                                customAnnotation.Id = annotation.Id.ToString();

                                if (annotation.Incident_Annotation != null)
                                {
                                    customAnnotation.ServiceRequestId = annotation.Incident_Annotation.Id.ToString();
                                }

                                customAnnotation.Message = annotation.NoteText;
                                customAnnotation.CreatedOn = (DateTime)annotation.CreatedOn;
                                customAnnotation.UserId = annotation.CreatedBy.Id.ToString();

                                annotationsList.Add(customAnnotation);
                            }
                        }
                    }
            }
            catch (Exception e)
            {
                throw e;
            }

            return annotationsList;
        }

为什么我在尝试发生事件时会收到此错误.Incident_Annotation?我认为incident.Incident_Annotation是NULL,但为什么呢?我的事件最少有1个或更多注释。

1 个答案:

答案 0 :(得分:0)

必须显式加载相关实体,您可以在此MSDN文章中找到更多信息:

MSDN - Access Entity Relationships