在 ASP.NET Core 上为 Entity Framework 使用不同的连接提供程序
Using a different connection provider for Entity Framework on ASP.NET Core
我正在使用 ASP.NET Core 1.1 和 Entity Framework 6 连接到 SQL Server Compact 3.5 数据库。如何配置创建 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;",
}
}
我的代码导致了这个错误:
ArgumentException: Keyword not supported: 'provider'
SQL Server Compact 尚未在 .net core 中开箱即用,因此它涉及的工作比简单地编辑提供程序以使其正常工作要多一些。您需要为它创建一个模型。
您可以使用 EntityFramework.SqlServerCompact package 并使用他们的示例模型作为模板。
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; }
}
}
希望对您有所帮助。
您需要在继承自 DbConfiguration
的 class 中指定提供商。
SqlCeDbConfiguration
public class SqlCeDbConfiguration: DbConfiguration
{
public DbConfig()
{
SetProviderServices("System.Data.SqlServerCe.3.5", System.Data.Entity.SqlServerCompact.Legacy.SqlCeProviderServices.Instance);
}
}
然后您可以将 DbConfigurationType
属性应用于 DbContext
派生的 class
MyDbContext.cs
[DbConfigurationType(typeof(SqlCeDbConfiguration))]
public class MyDbContext: DbContext
{
// ...
}
我正在使用 ASP.NET Core 1.1 和 Entity Framework 6 连接到 SQL Server Compact 3.5 数据库。如何配置创建 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;",
}
}
我的代码导致了这个错误:
ArgumentException: Keyword not supported: 'provider'
SQL Server Compact 尚未在 .net core 中开箱即用,因此它涉及的工作比简单地编辑提供程序以使其正常工作要多一些。您需要为它创建一个模型。
您可以使用 EntityFramework.SqlServerCompact package 并使用他们的示例模型作为模板。
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; }
}
}
希望对您有所帮助。
您需要在继承自 DbConfiguration
的 class 中指定提供商。
SqlCeDbConfiguration
public class SqlCeDbConfiguration: DbConfiguration
{
public DbConfig()
{
SetProviderServices("System.Data.SqlServerCe.3.5", System.Data.Entity.SqlServerCompact.Legacy.SqlCeProviderServices.Instance);
}
}
然后您可以将 DbConfigurationType
属性应用于 DbContext
派生的 class
MyDbContext.cs
[DbConfigurationType(typeof(SqlCeDbConfiguration))]
public class MyDbContext: DbContext
{
// ...
}