I'm using crmsvcutil to generate early bound types. In the crm 4.0 days one was able to load related entites just by hitting the entity.ChildEntities property.
//Winvs.Next.Entities.CrmDataContext dc = new Entities.CrmDataContext(new Microsoft.Xrm.Sdk.Client.OrganizationServiceContext(
var cred = new System.ServiceModel.Description.ClientCredentials();
cred.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
//
using (var organizationServiceProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(new Uri(System.Configuration.ConfigurationManager.ConnectionStrings["CrmConnection"].ConnectionString), null, cred, null))
using (Winvs.Next.Entities.CrmDataContext dc = new Entities.CrmDataContext(organizationServiceProxy))
{
// This statement is required to enable early-bound type support.
organizationServiceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior());
//
foreach (var a in dc.AccountSet)
{
foreach (var c in a.contact_customer_accounts)
{
c.FullName.ToString();
}
}
}
When I do this with the latest CRM 2011 SDK version instead of loading realted entities I'm getting a NullReferenceException which gives me no further information about the issue.
foreach (var c in a.contact_customer_accounts)
What Do i miss? How can I load related entities with CRM2011 Linq?
I was having exactly the same issue as you. I saw those properties and was perplexed as to why they always returned null
. They clearly were intended to retrieve entities for a particular relationship, yet they seemed to be "dormant."
It occurred to me that maybe the context object was not configured properly to lazy-load these "navigation" properties (to borrow a term from Entity Framework). So I started researching what I could about OrganizationServiceContext
, and found this bit about its LoadProperty
method:
If the property represents an association, link or deferred property, calling this method provides the client a way to lazily load related resources.
That sounded like what I needed, and one of the overloads takes an Entity
and a Relationship
as input. So, once you have an instance of an entity with one or more relationships, you need to ask the context to load the corresponding properties. Keep in mind, the entity must be attached to the context, either automatically (because you retrieved the entity via a context query), or manually using the Attach
method.
I'm a little confused by your code because you're using a CrmDataContext
object rather than an OrganizationServiceContext
object. The code samples in the 2011 SDK use the latter, and the crmsvcutil will even generate a custom instance of OrganizationServiceContext
with methods in the form of "[ENTITY_NAME]Set" (i.e. AccountSet
as in your example). You may need to switch over to the newer context type.
So, using your example and assuming dc
is now an instance of OrganizationServiceContext
, it would look like:
Relationship contactRel = new Relationship("contact_customer_accounts");
foreach (var a in dc.AccountSet) {
dc.LoadProperty(a, contactRel); // Tell context to load entities from this relationship
foreach (var c in a.contact_customer_accounts) {
c.FullName.ToString();
}
}
It is a pain to have to manually load each relationship, but I can find no other way of activating those properties.
Note: To get crmsvcutil to generate a custom OrganizationServiceContext
, specify the serviceContextName switch:
crmsvcutil.exe /url:<your_crm_url> /out:Xrm.cs /serviceContextName:XrmServiceContext
This would create a derived class named XrmServiceContext
with accessors for all of the different entity types in your organization.
I really have no clue why this is the way it is but it turned out after some researching sessions that one has to use the xrm provided code customization assembly to generate the entity object model classes.
There is a SDK Version 5.0.4 sample provided which builds a sample console showing off how to generate and consume the entity classes in a way we're all used from the CRM 4.0 xrm days.
To make a long story short I post the crmsvcutil call here, for further information you should consult the sdk sample Walkthrough: Build a Console Application That Connects to Microsoft Dynamics CRM 2011 Using Developer Extensions http://technet.microsoft.com/en-us/library/gg695803.aspx
CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm\Xrm.cs /url:http://Crm/Contoso/XRMServices/2011/Organization.svc /domain:CONTOSO /username:administrator /password:pass@word1 /namespace:Xrm /serviceContextName:XrmServiceContext
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With