![]() |
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 HubDB data 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 HubDB Entity Framework 6 assembly and the connection string.
There are two authentication methods available for connecting to HubDB data source: OAuth Authentication with a public HubSpot application and authentication with a Private application token.
AuthScheme must be set to "OAuth" in all OAuth flows. Be sure to review the Help documentation for the required connection properties for you specific authentication needs (desktop applications, web applications, and headless machines).
Follow the steps below to register an application and obtain the OAuth client credentials:
Under Scopes, select any scopes you need for your application's intended functionality.
A minimum of the following scopes is required to access tables:
To connect using a HubSpot private application token, set the AuthScheme property to "PrivateApp."
You can generate a private application token by following the steps below:
To connect, set PrivateAppToken to the private application token you retrieved.
<configuration> ... <connectionStrings> <add name="HubDBContext" connectionString="Offline=False;AuthScheme=OAuth;OAuthClientID=MyOAuthClientID;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH;" providerName="System.Data.CData.HubDB" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.HubDB" type="System.Data.CData.HubDB.HubDBProviderServices, System.Data.CData.HubDB.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class HubDBContext : DbContext {
public HubDBContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// To remove the requests to the Migration History table
Database.SetInitializer<HubDBContext>(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("NorthwindProducts")]
public class NorthwindProducts {
[System.ComponentModel.DataAnnotations.Key]
public System.String PartitionKey { get; set; }
public System.String Name { get; set; }
}
public DbSet<NorthwindProducts> NorthwindProducts { set; get; }
HubDBContext context = new HubDBContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.NorthwindProducts select line;
Download a free trial of the HubDB Data Provider to get started:
Download NowLearn more:
👁 HubDB IconRapidly create and deploy powerful .NET applications that integrate with HubDB.