Random Dev Notes

November 7, 2011

VFP Entity Framework Provider (v0.6)

Filed under: Development — Tags: — Tom Brothers @ 1:06 am


Another VFP EF Provider build is ready for testing.  The focus of this build was to get the provider to work with “Code First” scenarios.

The remainder of this blog entry will walk you though a simple example of using Code First.  This example was derived from the ADO.NET team blog – EF 4.2 Code First Walkthrough.

Create the Application

  • Open Visual Studio 2010
  • File –> New –> Project..
  • Select “Windows” from the left menu and “Console Application”
  • Enter “CodeFirstSample” as the name
  • Select “OK”

Add References

  • Add EntityFramework.dll
  • Add VfpEntityFrameworkProvider.dll

Add Code

Replace the Project.cs code with the following:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
namespace CodeFirstSample {
    class Program {
        static void Main(string[] args) {
            using (var db = new ProductContext()) {
                // Use Find to locate the Food category 
                var food = db.Categories.Find("FOOD");
                if (food == null) {
                    food = new Category { CategoryId = "FOOD", Name = "Foods" };
                // Create a new Food product 
                Console.Write("Please enter a name for a new food: ");
                var productName = Console.ReadLine();
                var product = new Product { Name = productName, Category = food };
                int recordsAffected = db.SaveChanges();
                Console.WriteLine("Saved {0} entities to the database.", recordsAffected);
                // Query for all Food products using LINQ 
                var allFoods = from p in db.Products
                               where p.CategoryId == "FOOD"
                               orderby p.Name
                               select p;
                Console.WriteLine("All foods in database:");
                foreach (var item in allFoods) {
                    Console.WriteLine(" - {0}", item.Name);
                Console.WriteLine("Press any key to exit.");
    public class ProductContext : DbContext {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    public class Category {
        public string CategoryId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Product> Products { get; set; }
    public class Product {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string CategoryId { get; set; }
        public virtual Category Category { get; set; }

This example deviates from the ADO.NET team blog – EF 4.2 Code First Walkthrough when defining the model classes (Category and Product).  The MaxLength attribute was added to let the provider know that the table field should be varchar instead of a memo.

Change App.Config

Add the provider information and connection string to app.config.

<?xml version="1.0" encoding="utf-8" ?>
      <clear />
      <add name="Vfp Entity Framework Provider"
                 description="Vfp Entity Framework Provider"
                 type="VfpEntityFrameworkProvider.VfpProviderFactory, VfpEntityFrameworkProvider, Version=, Culture=neutral, PublicKeyToken=feace53afe38fe48" />
    <add name="ProductContext"
             connectionString="provider=vfpoledb;data source=DataCodeFirst.dbc"
             providerName="VfpEntityFrameworkProvider" />

Run & Review

Run the application and then check the database container directory to see that the data files have been created.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: