Entity Framework for DynamicCRM

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
manoj241176
Posts: 2
Joined: Thu 07 Jul 2016 13:20

Entity Framework for DynamicCRM

Post by manoj241176 » Thu 07 Jul 2016 13:32

Hi,

Can you please suggest why to use Entity Framework for Dynamic CRM, where in we can directly call the Dynamic CRM IOrganizationServices, and get the data using "FetchXML".

Are we not creating another wrapper over the Dynamic CRM Services, and would not it create a performance impact?

What benefit we would get by using devart entity framework tool for Dynamic CRM, with respect to development, performance of query etc.

Please help me understand this, so that I can look for the usage of this tool.

Best Regards
Manoj

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Entity Framework for DynamicCRM

Post by Shalex » Fri 08 Jul 2016 15:59

Entity Framework gives a higher level of abstraction and increases a speed of development.

Cloud services (including Dynamics CRM) provide a limited API functionality via queries. Our provider has an embedded cache and, therefore, can execute most of queries generated by Entity Framework engine. Additionally, you can run SQL-92 compliant statements via ADO.NET interface: https://www.devart.com/dotconnect/dynam ... ement.html.

manoj241176
Posts: 2
Joined: Thu 07 Jul 2016 13:20

Re: Entity Framework for DynamicCRM

Post by manoj241176 » Mon 11 Jul 2016 07:35

I am not using Cloud service of Dynamic CRM 2015, we are using On Premise version of Dynamic CRM 2015.

I have done a PoC on performance comparision between FetchXML and Devart ADO.NET Provider for Dynamics CRM with Entity Framework, where in I have found that FetchXML is performing better as compared to devart driver.

Time Taken by FetchXML: - 39445(milisec) for 42 rows
Time Taken by Devart : - 49279(milisec) for 42 rows

Find below code for both approach's and suggest if there is anything which can be done to perform better.

---------------------------------------DevArt ADO.NEt Provider for Dynamic CRM with Entity Framework--------

Code: Select all


 class Program
    {
        static void Main(string[] args)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            Entities context = new Entities();
            var query = from cnt in context.contact
                        orderby cnt.firstname, cnt.lastname
                        select cnt;
            int i = 0;
            foreach (contact con in query)
            {
                i++;
                Console.WriteLine("{0} | {1} | {2}", con.contactid, con.firstname, con.lastname);
            }
                
            watch.Stop();
            Console.WriteLine("Record Count: " + i.ToString());
            Console.WriteLine("Time Taken: " + watch.ElapsedMilliseconds.ToString());

            Console.ReadLine();
    
        }
    }

---------------------------------------FetchXML Query--------
We have used below helper files / classes provided by Microsoft.

CRMServices\crmservicehelpers.cs
CRMServices\deviceidmanager.cs
CRMServices\myorganizationcrmsdktypes.cs

Code: Select all




class Program
    {
        static void Main(string[] args)
        {
            FetchData(Singleton.Service);
            Console.ReadLine();
        }

        static void FetchData(IOrganizationService _Service)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            string contactQuery = @"<fetch mapping='logical' version='1.0'>
                                    <entity name='contact'>
                                    <attribute name='contactid' />
                                    <attribute name='firstname' />
                                    <attribute name='lastname' />
                                    
                                    </entity></fetch>";

            EntityCollection associatedContacts_result = _Service.RetrieveMultiple(new FetchExpression(contactQuery));
            int i = 0;
            foreach (var c in associatedContacts_result.Entities)
            {
                i++;
                string contactId = string.Empty, firstName = string.Empty, lastName = string.Empty;
                
                if (!string.IsNullOrEmpty(((Guid)c.Attributes["contactid"]).ToString()))
                    contactId = ((Guid)c.Attributes["contactid"]).ToString();

                if (!string.IsNullOrEmpty((string)c.Attributes["firstname"]))
                    firstName = ((string)c.Attributes["firstname"]);
                if(!string.IsNullOrEmpty(((Contact)c).FirstName.ToString()))
                    lastName = ((Contact)c).FirstName.ToString();
                Console.WriteLine("{0} | {1} | {2}", contactId, firstName, lastName);
            }
            watch.Stop();
            Console.WriteLine("Record Count=" +i.ToString());
            Console.WriteLine("Time Taken: " + watch.ElapsedMilliseconds.ToString());
        }
    }

    public class Singleton
    {
        private static IOrganizationService _Service = null;
        private Singleton()
        { }
        private static object lockThis = new object();
        public static IOrganizationService Service
        {
            get
            {
                lock (lockThis)
                {
                    if (_Service == null)
                    {
                        _Service = getService();
                    }
                    return _Service;
                }
            }
        }

        public static IOrganizationService getService()
        {
            ServerConnection serverConnect = new ServerConnection();
            ServerConnection.Configuration config = serverConnect.GetServerConfiguration();
            OrganizationServiceProxy _serviceProxy = null;

            _serviceProxy = new OrganizationServiceProxy(config.OrganizationUri,
                                                         config.HomeRealmUri,
                                                         config.Credentials,
                                                         config.DeviceCredentials)
            { Timeout = TimeSpan.FromMilliseconds(200000) };

            _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

            IOrganizationService service = (IOrganizationService)_serviceProxy;
            return service;
        }
    }


Please suggest how this can be done better.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Entity Framework for DynamicCRM

Post by Shalex » Wed 13 Jul 2016 16:38

1. This article describes performance characteristics of the ADO.NET Entity Framework and provides some considerations to help improve the performance of Entity Framework applications: https://msdn.microsoft.com/en-us/librar ... 10%29.aspx.

2. Try using a plain ADO.NET: https://www.devart.com/dotconnect/dynam ... eving.html.

Post Reply