![]() |
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 Excel Online 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 Excel Online Entity Framework 6 assembly and the connection string.
You can connect to a workbook by providing authentication to Excel Online and then setting the following properties:
: Set this to the name or Id of the workbook.
If you want to view a list of information about the available workbooks, execute a query to the Workbooks view after you authenticate.
You use the OAuth authentication standard to authenticate to Excel Online. See the Getting Started section in the help documentation for a guide. Getting Started also guides you through executing SQL to worksheets and ranges.
<configuration> ... <connectionStrings> <add name="ExcelOnlineContext" connectionString="Offline=False;InitiateOAuth=GETANDREFRESH;" providerName="System.Data.CData.ExcelOnline" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.ExcelOnline" type="System.Data.CData.ExcelOnline.ExcelOnlineProviderServices, System.Data.CData.ExcelOnline.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class ExcelOnlineContext : DbContext {
public ExcelOnlineContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// To remove the requests to the Migration History table
Database.SetInitializer<ExcelOnlineContext>(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("Test_xlsx_Sheet1")]
public class Test_xlsx_Sheet1 {
[System.ComponentModel.DataAnnotations.Key]
public System.String Id { get; set; }
public System.String Column1 { get; set; }
}
public DbSet<Test_xlsx_Sheet1> Test_xlsx_Sheet1 { set; get; }
ExcelOnlineContext context = new ExcelOnlineContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Test_xlsx_Sheet1 select line;
Download a free trial of the Excel Online Data Provider to get started:
Download NowLearn more:
👁 Excel Online IconRapidly create and deploy powerful .NET applications that integrate with live Excel Online Spreadsheet data!