Using a different connection provider for Entity Framework on ASP.NET Core

2k views Asked by At

I'm using ASP.NET Core 1.1 with Entity Framework 6 to connect to a SQL Server Compact 3.5 database. How can I configure what provider to use when creating the DbContext?

MyDbContext.cs

public class MyDbContext: DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // omitted
    }

    public static DbMasContext Create(string connString)
    {
        var entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.ProviderConnectionString = connString;
        entityBuilder.Provider = "System.Data.SqlServerCe.3.5";
        return new DbMasContext(entityBuilder.ConnectionString);
    }
}

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddScoped(_ => DbMasContext.Create(Configuration.GetConnectionString("MyDbContext")));
    }

    // omitted
}

appsettings.json

{
  "ConnectionStrings": {
    "MyDbContext": "DataSource=C:\\MyDb.sdf;Max Database Size=2048;",
  }
}

My code causes this error:

ArgumentException: Keyword not supported: 'provider'

2

There are 2 answers

0
Lucius On BEST ANSWER

You need to specify the provider in a class that inherits from DbConfiguration.

SqlCeDbConfiguration

public class SqlCeDbConfiguration: DbConfiguration
{
    public DbConfig()
    {
        SetProviderServices("System.Data.SqlServerCe.3.5", System.Data.Entity.SqlServerCompact.Legacy.SqlCeProviderServices.Instance);
    }
}

Then you can apply a DbConfigurationType attribute to your DbContext-derived class

MyDbContext.cs

[DbConfigurationType(typeof(SqlCeDbConfiguration))]
public class MyDbContext: DbContext
{
    // ...
}
1
TidyDev On

SQL Server Compact dosen't yet work out of the box in .net core, so it involves a little bit more work than simply editing the provider to get it working. You need to create a model for it to work.

You can use the EntityFramework.SqlServerCompact package and use their example model as a template.

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.UseSqlCe(@"Data Source=C:\data\Blogging.sdf");
    }

    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; }
}
}

Hope this helps.