Accessing QuickBooks with Entity Framework

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 for yourself, this 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 we will demonstrate a Code First approach to accessing your QuickBooks Data using the RSSBus ADO.NET Provider.

Even though this article uses the QuickBooks Data Provider, the exact same procedure outlined below can be used with any RSSBus ADO.NET Data Providers to access data through Entity Framework.

  • Step 1: Open Visual Studio and create a new Windows Form project. For the purposes of this article, we will be using a C# project.
  • Step 2: Add to the project an app.config file. In this file, add the connection string and provider like so:
<connectionStrings>
	<add name="QuickBooksContext" connectionString="Offline=False;User=USERNAME;Password=PASSWORD;URL=http://localhost:2080;QBXML Version=6.0" providerName="System.Data.RSSBus.QuickBooks" />
</connectionStrings>
  • Step 3: Add a reference to System.Data.Entity and EntityFramework in your project. You may need to download the EntityFramework.dll from Microsoft. In this demo we are using Entity Framework version 4.1.
  • Step 4: 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. You will also need to override the OnModelCreating method and remove PluralizaingTableNameConvention and IncludeMetadataConvention from the ModelBuilder Conventions:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
    // To remove the plural names
            
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    // To remove calls to EdmTable a metadata table.
    // If this is not removed EF will try to describe and select from EdmTable
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
 }
  • Step 5: Create another .cs file and name it after the QuickBooks entity you are retrieving. In our example, we are retrieving Accounts. In this file you will need to define both the Entity and the Entity Configuration. They will look something like this:
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);
    }
  • Step 6: Now that you have created an entity, go back to your context class and add the entity to your Context like so:
public DbSet<Accounts> Account { set; get; }
  • Step 7: With the context and entity finished, you are now read to query the data in a separate class. For example:
QuickBooksContext context = new QuickBooksContext();
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.

 
 
Products