Skip to content

Using Entity Framework Core with Jet in Traditional .Net Applications

Bubi edited this page Sep 16, 2017 · 3 revisions

When using the Jet Entity Framework Core provider there are some Limitations about migrations, data types and target cpu. So please check them before start using the provider.
There are also other limitations related to Jet Database Engine most related to the data size and the query complexity.

Latest version of NuGet Package Manager and PowerShell

If you wish to use Visual Studio 2015, you must install NuGet Package Manager version 3.6 or later

If you wish to use Visual Studio 2017, you must use Update 3 - version 15.3

Latest version of Windows PowerShell - only required on Windows 8.0/Windows Server 2012 and earlier

Install the Entity Framework Core Jet provider

To get the Jet Entity Framework Core provider in your project you need to install the package for the Jet provider from NuGet.org. Your project must target .NET Framework 4.6.1 or later.

Create Your Model

Define a context and classes that make up your model. Note the new OnConfiguring method that is used to specify the data store provider to use (and, optionally, other configuration too). You can also code generate a model from an existing database, see below.

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace Sample
{
    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseJet(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>()
                .HasMany(b => b.Posts)
                .WithOne(p => p.Blog)
                .HasForeignKey(p => p.BlogId);
        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
}

Create Your Database

Now that you have a model, you can use migrations to create a database for you.

In Package Manager Console (Tools –> NuGet Package Manager –> Package Manager Console):

Install-Package Microsoft.EntityFrameworkCore.Tools

to make the migrations commands available in your project.

Add-Migration MyFirstMigration

to scaffold a migration to create the initial set of tables for your model.

Update-Database

to apply the new migration to the database. Because your database doesn't exist yet, it will be created for you before the migration is applied.

If you make future changes to your model, you can use the Add-Migration command to scaffold a new migration to apply the corresponding changes to the database. Once you have checked the scaffolded code (and made any required changes), you can use the Update-Database command to apply the changes to the database.

Create a Model from an existing database

If you already have a database, your can use the Package Manager Console Scaffold-DbContext command to create your model classes from an existing database. This feature is only available in Visual Studio 2015.

To do this, you must add a couple of NuGet packages to your project:

In Package Manager Console (Tools –> NuGet Package Manager –> Package Manager Console):

Install-Package Microsoft.EntityFrameworkCore.Tools  

to make the reverse engineer command available in Package Manager Console. (If not already installed in the section above)

Scaffold-DbContext -Connection "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;" -Provider EntityFrameworkCore.Jet

to generate DbContext and POCO classes in your project from your existing database.

Use Your Model

You can now use your model to perform data access.

using System;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new BloggingContext())
            {
                db.Blogs.Add(new Blog { Url = "http://myblog.com" });
                db.SaveChanges();

                foreach (var blog in db.Blogs)
                {
                    Console.WriteLine(blog.Url);
                }
                Console.ReadKey();
            }
        }
    }
}