Accessing QuickBooks with Entity Framework 6



Entity Framework is an object-relational mapping framework that can be used to work with data as objects. While you can run the ADO.NET Entity Data Model wizard in Visual Studio to handle generating the Entity Model, this approach, the model-first approach, can put you at a disadvantage if there are changes in your data source or if you want more control over how the entities operate. In this article you will complete the code-first approach to accessing QuickBooks data using the CData ADO.NET Provider.

The procedure outlined below can be used with any CData ADO.NET Data Providers to access data through Entity Framework 6. See the help documentation for a guide to using the model-first approach.

  1. Open Visual Studio and create a new Windows Form Application. This article uses a C# project with .NET 4.5.
  2. Run the command 'Install-Package EntityFramework' in the Package Manger Console in Visual Studio to install the latest release of Entity Framework.
  3. Modify the App.config file in the project to add a reference to the QuickBooks Entity Framework 6 assembly and the connection string.
    <configuration>
      <!-- ... -->
      <connectionStrings>
        <add name="QuickBooksContext" connectionString="Offline=False;User=USERNAME;Password=PASSWORD;URL=http://localhost:2080;QBXML Version=12.0" providerName="System.Data.CData.QuickBooks" />
      </connectionStrings>
      <entityFramework>
        <providers>
          <!-- ... -->
          <provider invariantName="System.Data.CData.QuickBooks" type="System.Data.CData.QuickBooks.QuickBooksProviderServices, System.Data.CData.QuickBooks.Entities.EF6" />
        </providers>
      <entityFramework>
    </configuration>
    To connect to Desktop editions of QuickBooks, use the Remote Connector application installed with the application. The Remote Connector is a lightweight, stand-alone server that enables you to connect to remote QuickBooks instances. It is also used to connect your application to QuickBooks in situations where direct COM access to QuickBooks is not available (e.g., ASP.NET, Java, or a company file on a remote machine). For more information and a step-by-step guide to establish a connection, refer to the "Getting Started" guide in the help documentation.
  4. Add a reference to System.Data.CData.QuickBooks.Entities.EF6.dll, located in the lib -> 4.0 subfolder in the installation directory, and build the project to complete the setup for using Entity Framework 6.
  5. Build the project at this point to ensure everything is working correctly.

Follow the steps below to start coding using Entity Framework:

  1. Add a new .cs file to the project and add a class to it. This will be your database context, and it will extend the DbContext class. In the example, this class is named QuickBooksContext. The following code example overrides the OnModelCreating method to make the following changes:
    • Remove PluralizingTableNameConvention from the ModelBuilder Conventions.
    • Remove requests to the MigrationHistory table.
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.ModelConfiguration.Conventions;
    
    class QuickBooksContext : DbContext {
      public QuickBooksContext() { }
    
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
        // To remove the requests to the Migration History table
        Database.SetInitializer<QuickBooksContext>(null);  
        // To remove the plural names    
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
      }  
    }
  2. Create another .cs file and name it after the QuickBooks entity you are retrieving, for example, Accounts. In this file, define both the Entity and the Entity Configuration, which will resemble the example below:
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    
    public class Accounts {
    
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public System.String ID { get; set; }
      public System.String Name { get; set; }
      public System.String Type { get; set; } 
      public System.Double Balance { get; set; }
      public System.DateTime TimeCreated { get; set; }
    }
        
    public class AccountMap : EntityTypeConfiguration<Accounts> {
      public AccountMap() {
        this.ToTable("Accounts");
        this.HasKey(Account => Account.ID);
        this.Property(Account => Account.Name);
        this.Property(Account => Account.Type);
        this.Property(Account => Account.Balance);
        this.Property(Account => Account.TimeCreated);
      }
    }
  3. Now that you have created an entity, add the entity to your context class:
    public DbSet<Accounts> Account { set; get; }
  4. With the context and entity finished, you are now ready to query the data in a separate class. For example:
    QuickBooksContext context = new QuickBooksContext();
    context.Configuration.UseDatabaseNullSemantics = true;
    var query = from line in context.Account select line;

Sample Project

To help you get started using the QuickBooks Data Provider with Entity Framework code-first approach, download the fully functional sample project. The project includes examples of selecting, inserting, updating, and deleting data from QuickBooks. You will also need the QuickBooks ADO.NET Data Provider to make the connection. You can download a free trial here.

 
 
Downloads