问题描述
我有一个.Net Core WebApplication项目,其中上下文类位于类库中.如果我在OnConfiguring(DbContextOptionsBuilder optionsBuilder)方法中对连接字符串进行硬编码,则可以生成迁移.因为最好让依赖注入来管理上下文,所以我想将其添加到Startup Class中.但是,当我这样做时,会出现以下错误:
I have a .Net Core WebApplication Project in which the Context Class is in a Class Library. If I hard code the connection string in the OnConfiguring(DbContextOptionsBuilder optionsBuilder) method I can generate migrations. Since it is better to let dependency injection manage the context I would like to add this to the Startup Class. However when I do I get the following error:
DbContext类:
public class CustomerManagerContext : IdentityDbContext<User, Role, long, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
public CustomerManagerContext() { }
public CustomerManagerContext(DbContextOptions<CustomerManagerContext> options) : base(options)
{
}
//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//{
// base.OnConfiguring(optionsBuilder);
// optionsBuilder.UseSqlServer("SecretConnectionString");
//}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>().ToTable("Users");
builder.Entity<Role>().ToTable("Roles");
builder.Entity<UserClaim>().ToTable("UserClaims");
builder.Entity<UserRole>().ToTable("UserRoles");
builder.Entity<UserLogin>().ToTable("UserLogins");
builder.Entity<RoleClaim>().ToTable("RoleClaims");
builder.Entity<UserToken>().ToTable("UserTokens");
}
}
启动类-ConfigureServices方法
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<CustomerManagerContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
);
services.AddEntityFrameworkSqlServer()
.AddDbContext<CustomerManagerContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<CustomerManagerContext>()
.AddDefaultTokenProviders();
}
推荐答案
这让我很难受,得到了如下错误:
This bit me hard, getting errors like:
-
No database provider has been configured for this DbContext.
-
No design-time services were found.
-
The server was not found or was not accessible.
No database provider has been configured for this DbContext.
No design-time services were found.
The server was not found or was not accessible.
但是我最终得到了一个相当简单的解决方案/解决方法:
But I ended up with a fairly simple solution/work-around:
- 在解决方案(或命令行)中设置默认的启动项目 您的
-
添加迁移项目 :
Startup.cs
中的- Set the default startup-project in your solution (or in your command line)
in your
Startup.cs
add the migration-project:
public void ConfigureServices(IServiceCollection services)
{
var myDbContextAssemblyName = typeof(MyDbContext).Assembly.GetName().Name;
var connectionString = Configuration.GetConnectionString(MyDbContext.ConnectionStringName);
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(
connectionString,
x => x.MigrationsAssembly(myDbContextAssemblyName)));
// do more ...
}
在您的连接字符串中使用 IP地址和端口号(受此corefx问题),而不是服务器名称/dns(FWIW:).所以现在我的appsettings.Development.json
中有此内容:
in your connection-string use the IP-address and port-number (inspired by this corefx issue), instead of the server-name/dns (FWIW: query to get IP).So now I have this in my appsettings.Development.json
:
"ConnectionStrings":{ "MyConnectionStringName":数据源= 10.1.2.3,1433;初始目录= MyCatalog;集成安全性= SSPI" }
"ConnectionStrings": { "MyConnectionStringName": "Data Source=10.1.2.3,1433; Initial Catalog=MyCatalog; Integrated Security=SSPI" }
我发现了很多其他建议,并且我还会提到一些看起来很有趣的建议.也许会帮助其他人:
I found a lot of other suggestions, and I will mention a few that seemed interesting. Maybe it will help someone else:
在命令行中提及启动项目和迁移项目
Mention startup-project and migration-project in command-line:
Update-Database -Verbose -Project x.Data -StartupProject x.Web
从代码迁移
也可以在启动,"用于具有本地数据库的应用程序". (我想否则会在多个节点上运行,是否可能同时出现并发问题而启动多个运行时迁移?)
Migrate from code
One can also call migrations at StartUp, "for apps with a local database". (I guess otherwise running on multiple nodes, may start multiple runtime migrations at the same time with concurrency issues?)
myDbContext.Database.Migrate();
在Main.cs中设置DbContext
这对我来说不是必需的,因为.Net Core 2 在后台加载配置.
This is not necessary for me, I guess because .Net Core 2 loads the configuration behind the scenes.
对我来说这似乎太过分了.
This seems too much a hack to me.
Microsoft文档提到:
他们提到了提供这种设计时DbContext的这些方法:
They mention these ways to provide this design time DbContext:
-
来自应用程序服务:对于作为启动项目的ASP.NET Core应用程序:
from application services: for an ASP.NET Core app as startup project:
使用没有参数的构造函数
Using a constructor with no parameters
在设计时工厂
From a design-time factory
这篇关于EFCore无法识别数据库提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!