![]() |
VOOZH | about |
Microsoft Entity Framework serves as an object-relational mapping framework for working with data represented as objects. Although Visual Studio offers the ADO.NET Entity Data Model wizard to automatically generate the Entity Model, this model-first approach may present challenges when your data source undergoes changes or when you require greater control over entity operations. In this article, we will delve into the code-first approach for accessing Microsoft Dataverse data through the CData ADO.NET Provider, providing you with more flexibility and control.
CData provides the easiest way to access and integrate live data from Microsoft Dataverse (formerly the Common Data Service). Customers use CData connectivity to:
CData customers use our Dataverse connectivity solutions for a variety of reasons, whether they're looking to replicate their data into a data warehouse (alongside other data sources)or analyze live Dataverse data from their preferred data tools inside the Microsoft ecosystem (Power BI, Excel, etc.) or with external tools (Tableau, Looker, etc.).
Modify the App.config file in the project to add a reference to the Microsoft Dataverse Entity Framework 6 assembly and the connection string.
You can connect without setting any connection properties for your user credentials. Below are the minimum connection properties required to connect.
When you connect the Common Data Service OAuth endpoint opens in your default browser. Log in and grant permissions. The OAuth process completes automatically.
<configuration> ... <connectionStrings> <add name="CDSContext" connectionString="Offline=False;OrganizationUrl=https://myaccount.crm.dynamics.com/;InitiateOAuth=GETANDREFRESH;" providerName="System.Data.CData.CDS" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.CDS" type="System.Data.CData.CDS.CDSProviderServices, System.Data.CData.CDS.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class CDSContext : DbContext {
public CDSContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// To remove the requests to the Migration History table
Database.SetInitializer<CDSContext>(null);
// To remove the plural names
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
[System.ComponentModel.DataAnnotations.Schema.Table("Accounts")]
public class Accounts {
[System.ComponentModel.DataAnnotations.Key]
public System.String AccountId { get; set; }
public System.String Name { get; set; }
}
public DbSet<Accounts> Accounts { set; get; }
CDSContext context = new CDSContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Accounts select line;
Download a free trial of the Microsoft Dataverse Data Provider to get started:
Download NowLearn more:
👁 Microsoft Dataverse IconRapidly create and deploy powerful .NET applications that integrate with Microsoft Dataverse.