1

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
  • 1
    What's the value of entityBuilder.ConnectionString Commented Sep 4, 2017 at 10:21
  • provider=System.Data.SqlServerCe.3.5;provider connection string="DataSource=C:\MyDb.sdf;Max Database Size=2048;" Commented Sep 5, 2017 at 1:35

2 Answers 2

1

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
{
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

using the template i get 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlCe' ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.