共用方式為


Use XRM tooling with classes generated by using the code-generation tool

 

Applies To: Dynamics CRM 2013

The Microsoft.Xrm.Tooling.Connector assembly doesn’t directly provide interfaces for the entity and data context classes generated using the code-generation tool. However, you can use the Microsoft Dynamics CRM connection created by the CrmServiceClient class to set up your entity and data context classes by using the code-generation tool. More information: Generate code with the code generation tool extensions (Dynamics CRM 2013)

To use the CRM connection created by the CrmServiceClient class, create a connection to CRM by using an instance of this class, and then set the value of the OrganizationServiceProxy object to the CrmServiceClient.OrganizationServiceProxy property.

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>",“<Domain>”),"<Server>", "<Port>", "<OrgName>");

// Verify that you are connected.
if (crmSvc != null && crmSvc.IsReady)
{
    //Display the CRM version number and org name that you are connected to
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}", 
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

    Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy prox = crmSvc.OrganizationServiceProxy; 
}
else
{
    // Display the last error.
    Console.WriteLine("Error occurred: {0}", crmSvc.LastCrmError);

    // Display the last exception message if any.
    Console.WriteLine(crmSvc.LastCrmException.Message);
    Console.WriteLine(crmSvc.LastCrmException.Source);
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);

    return;
}

Note

The OrganizationServiceProxy class isn’t thread safe. While working with the entity and data context classes generated by using the code-generation tool or using .NET Language-Integrated Query (LINQ) to retrieve data, you might consider creating a locking scheme in your code if it runs in a multithreaded environment.

See Also

Use the IOrganizationService web service to read and write data or metadata
Build Windows client applications using the XRM tools