![]() |
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 OData services through the CData ADO.NET Provider, providing you with more flexibility and control.
CData simplifies access and integration of live OData services data. Our customers leverage CData connectivity to:
Customers use CData's solutions to regularly integrate their OData services with preferred tools, such as Power BI, MicroStrategy, or Tableau, and to replicate data from OData services to their databases or data warehouses.
Modify the App.config file in the project to add a reference to the OData Entity Framework 6 assembly and the connection string.
The User and Password properties, under the Authentication section, must be set to valid OData user credentials. In addition, specify a URL to a valid OData server organization root or OData services file.
<configuration> ... <connectionStrings> <add name="ODataContext" connectionString="Offline=False;URL=http://services.odata.org/V4/Northwind/Northwind.svc;UseIdUrl=True;OData Version=4.0;Data Format=ATOM;" providerName="System.Data.CData.OData" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.OData" type="System.Data.CData.OData.ODataProviderServices, System.Data.CData.OData.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class ODataContext : DbContext {
public ODataContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// To remove the requests to the Migration History table
Database.SetInitializer<ODataContext>(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("Orders")]
public class Orders {
[System.ComponentModel.DataAnnotations.Key]
public System.String OrderName { get; set; }
public System.String Freight { get; set; }
}
public DbSet<Orders> Orders { set; get; }
ODataContext context = new ODataContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Orders select line;
Download a free trial of the OData Data Provider to get started:
Download NowLearn more:
👁 OData IconEasy-to-use OData client (consumer) enables developers to build .NET applications that easily communicate with OData services.