具有配置的类库中的Entity Framework 7迁移脚手架 [英] Entity Framework 7 Migration Scaffolding in Class Library with Configuration
问题描述
尝试将迁移添加到驻留在ASP.NET 5类库中的EF7模型中.当运行dnx . ef migration add mymigration
时,失败的结果取决于我在哪个项目上运行.
Trying to add migrations to an EF7 Model that lives in an ASP.NET 5 class library. When running dnx . ef migration add mymigration
fails with different results depending on which project I run it on.
如果我在主项目的文件夹中运行它,则无法找到DbContext
,这是有道理的,因为DbContext
位于共享项目中,并且ef命令可能不在乎依赖项.
If I run it in the folder of the main project, it cannot locate a DbContext
, this makes sense because the DbContext
is in the shared project and ef commands likely don't care about dependencies.
如果我在共享项目的文件夹中运行它,则它无权访问startup.cs中指定的连接字符串.我从诸如此如果您在DbContext
的OnConfiguring
方法中指定了连接字符串,它确实可以在共享项目中工作,但是我真的很想将此代码与配置分开.
If I run it in the folder of the shared project, it does not have access to the connection string specified in the startup.cs. I have gleaned from questions like this that it does work from the shared project if you specify the connection string in the OnConfiguring
method of the DbContext
but I really would like to keep this code separate from the configuration.
我在EF7存储库中遇到了一些问题日志,其中提到他们实现了命令用于指定项目和上下文的行选项,但是没有示例,我无法通过查看提交历史记录中的源代码来弄清楚如何使用它.
I came across some issue logs in the EF7 repository that mention they implemented command line options for specifying a project and a context but there are no examples and I couldn't figure out how to use it from looking at the source code in the commit history.
推荐答案
这是一种可能对您有用的方法.
Here is an approach that might work for you.
如果我在共享项目的文件夹中运行它,则它无权访问startup.cs中指定的连接字符串.
If I run it in the folder of the shared project, it does not have access to the connection string specified in the startup.cs.
Startup.cs
我假设在您的Startup.cs中,您通过访问Configuration
而不是通过对其进行硬编码来指定连接字符串.此外,我假设在Startup.cs文件的构造函数中,您是从几个来源设置配置的.换句话说,您的Startup.cs可能看起来像这样:
Startup.cs
I'm assuming that in your Startup.cs, you're specifying the connection string by accessing Configuration
rather than by hard coding it. Further, I'm assuming that in your Startup.cs file's constructor, you're setting up configuration from a few sources. In other words, your Startup.cs might look something like this:
public class Startup
{
public IConfiguration Config { get; set; }
public Startup(IHostingEnvironment env)
{
var config = new Configuration()
.AddJsonFile("config.json")
.AddUserSecrets()
.AddEnvironmentVariables();
Config = config;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyDbContext>(options =>
{
options.UseSqlServer(Config["ConnectionStrings:MyDbContext"]);
});
}
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
{
var db = serviceProvider.GetRequiredService<MyDbContext>();
db.Database.AsSqlServer().EnsureCreated();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
config.json
此外,我假设您正在将连接字符串添加到项目根目录中的config.json中(或通过用户机密或环境变量来添加它.)您的config.json可能类似于这个:
config.json
Further, I'm assuming that you're adding the connection string to a config.json in the root of your project (or that you adding it via user secrets or environmental variables.) Your config.json might look something like this:
{
"ConnectionStrings": {
"MyDbContext": "Some-Connection-String"
}
}
如果您不那样做,可能值得尝试.
If you're not doing it that way, it might be worth trying it.
我从类似的问题中搜集到,如果您在DbContext的OnConfiguring方法中指定连接字符串,则它确实可以在共享项目中工作,但是我真的很想将此代码与配置分开.
I have gleaned from questions like this that it does work from the shared project if you specify the connection string in the OnConfiguring method of the DbContext but I really would like to keep this code separate from the configuration.
DbContext
如果上面的假设是正确的,则可以使用与Startup
类相同的模式来访问DbContext
中的连接字符串.也就是说,在DbContext
构造函数中,设置IConfiguration
.然后,在OnConfiguring
中访问连接字符串.它可能看起来像这样:
DbContext
If my assumptions above are correct, then you can access the connection string in the DbContext
by using the same pattern that you used in the Startup
class. That is, in the DbContext
constructor, setup the IConfiguration
. Then, in OnConfiguring
, access the connection string. It might look something like this:
public class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<SomeModel>().Key(e => e.Id);
base.OnModelCreating(builder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connString = Config["ConnectionStrings:MyDbContext"];
optionsBuilder.UseSqlServer(connString);
}
public IConfiguration Config { get; set; }
public MyDbContext()
{
var config = new Configuration()
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Config = config;
}
}
项目结构
您当然也需要在共享项目文件夹的根目录中有一个config.json文件.因此,您的项目的结构可能如下所示:
Project Structure
You'll of course need to have a config.json file in the root of your shared projects folder too. So, your projects' structure could look something like this:
SharedDataContext
Migrations
config.json
project.json
WebApp
config.json
project.json
Startup.cs
在上面,两个config.json文件都包含DbContext
的连接字符串设置.
In the above, both config.json files contain a connection string setting for the DbContext
.
如果您不喜欢复制config.json连接字符串,则可以使用环境变量或用户密码.
If you don't like duplicating the config.json connection string stuff, then you can use environmental variables or user secrets instead.
这篇关于具有配置的类库中的Entity Framework 7迁移脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!