Random Dev Notes

November 14, 2011

VFP Entity Framework Provider (v0.6.1)

Filed under: Development — Tags: — Tom Brothers @ 8:51 pm

I posted an update to the VFP EF Provider

Changes:

Bug:  Fixed an issue with getting the schema information when the connecting to free tables. 

Installer:  Modified the installer so that it will update the machine.config… so there is no need to manually change that machine.config anymore.

Installer:  Added a registry setting so that the provider is available in the Visual Studio “Add Reference” dialog box.

November 7, 2011

VFP Entity Framework Provider (v0.6)

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

Introduction

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
    image
  • 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" };
                    db.Categories.Add(food);
                }
                // 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 };
                db.Products.Add(product);
                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.");
                Console.ReadKey();
            }
        }
    }
 
    public class ProductContext : DbContext {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }
 
    public class Category {
        [MaxLength(10)]
        public string CategoryId { get; set; }
 
        [MaxLength(50)]
        public string Name { get; set; }
 
        public virtual ICollection<Product> Products { get; set; }
    }
 
    public class Product {
        public int ProductId { get; set; }
 
        [MaxLength(50)]
        public string Name { get; set; }
 
        [MaxLength(10)]
        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" ?>
<configuration>
  <system.data>
    <DbProviderFactories>
      <clear />
      <add name="Vfp Entity Framework Provider"
                 invariant="VfpEntityFrameworkProvider"
                 description="Vfp Entity Framework Provider"
                 type="VfpEntityFrameworkProvider.VfpProviderFactory, VfpEntityFrameworkProvider, Version=0.6.0.0, Culture=neutral, PublicKeyToken=feace53afe38fe48" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="ProductContext"
             connectionString="provider=vfpoledb;data source=DataCodeFirst.dbc"
             providerName="VfpEntityFrameworkProvider" />
  </connectionStrings>
</configuration>

Run & Review

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

Blog at WordPress.com.