![]() |
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 JSON services through the CData ADO.NET Provider, providing you with more flexibility and control.
Modify the App.config file in the project to add a reference to the JSON Entity Framework 6 assembly and the connection string.
See the Getting Started chapter in the data provider documentation to authenticate to your data source: The data provider models JSON APIs as bidirectional database tables and JSON files as read-only views (local files, files stored on popular cloud services, and FTP servers). The major authentication schemes are supported, including HTTP Basic, Digest, NTLM, OAuth, and FTP. See the Getting Started chapter in the data provider documentation for authentication guides.
After setting the and providing any authentication values, set to more closely match the data representation to the structure of your data.
The property is the controlling property over how your data is represented into tables and toggles the following basic configurations.
See the Modeling JSON Data chapter for more information on configuring the relational representation. You will also find the sample data used in the following examples. The data includes entries for people, the cars they own, and various maintenance services performed on those cars.
<configuration> ... <connectionStrings> <add name="JSONContext" connectionString="Offline=False;URI=C:/people.json;DataModel=Relational;" providerName="System.Data.CData.JSON" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.JSON" type="System.Data.CData.JSON.JSONProviderServices, System.Data.CData.JSON.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class JSONContext : DbContext {
public JSONContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// To remove the requests to the Migration History table
Database.SetInitializer<JSONContext>(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("people")]
public class people {
[System.ComponentModel.DataAnnotations.Key]
public System.String [ personal.name.first ] { get; set; }
public System.String [ personal.name.last ] { get; set; }
}
public DbSet<people> people { set; get; }
JSONContext context = new JSONContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.people select line;
Download a free trial of the JSON Data Provider to get started:
Download NowLearn more:
👁 JSON IconRapidly create and deploy powerful .NET applications that integrate with JSON web services.