![]() |
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 IBM Cloud Object Storage 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 IBM Cloud Object Storage Entity Framework 6 assembly and the connection string.
If you do not already have Cloud Object Storage in your IBM Cloud account, follow the procedure below to install an instance of SQL Query in your account:
There are certain connection properties you need to set before you can connect. You can obtain these as follows:
To connect with IBM Cloud Object Storage, you need an API Key. You can obtain this as follows:
If you have multiple accounts, specify the CloudObjectStorageCRN explicitly. To find the appropriate value, you can:
You can now set the following to connect to data:
When you connect, the connector completes the OAuth process.
<configuration> ... <connectionStrings> <add name="IBMCloudObjectStorageContext" connectionString="Offline=False;ApiKey=myApiKey;CloudObjectStorageCRN=MyInstanceCRN;Region=myRegion;OAuthClientId=MyOAuthClientId;OAuthClientSecret=myOAuthClientSecret;" providerName="System.Data.CData.IBMCloudObjectStorage" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.IBMCloudObjectStorage" type="System.Data.CData.IBMCloudObjectStorage.IBMCloudObjectStorageProviderServices, System.Data.CData.IBMCloudObjectStorage.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class IBMCloudObjectStorageContext : DbContext {
public IBMCloudObjectStorageContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// To remove the requests to the Migration History table
Database.SetInitializer<IBMCloudObjectStorageContext>(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("Objects")]
public class Objects {
[System.ComponentModel.DataAnnotations.Key]
public System.String Key { get; set; }
public System.String Etag { get; set; }
}
public DbSet<Objects> Objects { set; get; }
IBMCloudObjectStorageContext context = new IBMCloudObjectStorageContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Objects select line;
Download a free trial of the IBM Cloud Object Storage Data Provider to get started:
Download NowLearn more:
👁 IBM Cloud Object Storage IconRapidly create and deploy powerful .NET applications that integrate with IBM Cloud Object Storage.