Now that the Common Data Service allows SQL connections to run queries against the entity tables, I thought I might play around with Entity Framework…
Sample Console Application
Let’s quickly build a sample application that uses Entity Framework to consume the CDS TDS endpoint.
Prerequisites
Build
Run the following .NET Core CLI command to create a new console application:
dotnet new console -n CdsEfSampleApp
Install the required packages with the following commands:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Json
dotnet add package Microsoft.Extensions.DependencyInjection
Add the following files to the project.
Don’t forget to replace the connection string placeholders in the appsettings.json file with your CDS environment details and credentials.
Add the following ItemGroup
to the CdsEfSampleApp.csproj
project file to ensure the appsettings.json
file is always copied to the bin folders when the project is built.
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
Run
Run the application.
dotnet run
Too lazy to build it yourself?
I’ve created a sample project that is available at https://github.com/ryanmichaeljames/cds-tds-ef-sample. The project contains a simple implemetaion of Entity Framework’s DbContext
that is used to query the contact table.
The README details the steps to get a copy of the sample code running on your local machine.
Closing Thoughts
Being able to use Entity Framework to query the Common Data Service’s entity tables opens up a lot of new possiblities. As a developer with a CRM development background this is kind of mind blowing and encourages out of the box thinking.