shk*_*per 19 entity-framework-core asp.net-core-mvc
我正在尝试使用entityframework.commands在EF7中使用迁移.但我的DbContext与启动项目处于不同的程序集(asp.net mvc是一个启动项目,Core.Implementation有一个DbContex).
dnx.ef migration添加MyMigration -c MyContext
System.InvalidOperationException:未找到名为"MyContext"的DbContext.
我试图使用命名空间指向其他程序集,但它也没有用.有可能吗?或者我只需要将我的上下文放在ef7命令所在的汇编中?
And*_*Gis 24
编辑
从1.0.0-rc1-final(可能更早)开始,这个工作区是不必要的
App.DataAccess/Startup.cs(如果你使用下面的解决方法,只需删除它)App.Web)-p包含迁移的项目(参数):cd App.Web
dnx ef migrations add NewMigration -p App.DataAccess
Run Code Online (Sandbox Code Playgroud)
如果您有多个数据库上下文,则还必须指定要使用的数据库上下文(-c参数)
cd App.Web
dnx ef migrations add NewMigration -p App.DataAccess -c YourDbContext
Run Code Online (Sandbox Code Playgroud)
编辑结束
我发现了一个解决方法
假设你有2个项目:App.Web和App.DataAccess
您可以为您的App.DataAccess以下内容添加一个非常基本的Startup类:
>App.Web
-->Startup.cs // your main startup
>App.DataAccess
-->path/to/ApplicationDbContext.cs
-->different/path/to/YourModels.cs
-->Startup.cs // add a simple startup class
-->project.json
Run Code Online (Sandbox Code Playgroud)
简单的Startup类(App.DataAccess\Startup.cs):
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Data.Entity;
using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
namespace App.DataAccess
{
public class Startup
{
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("../App.Web/config.json"); // path to your original configuration in Web project
Configuration = builder.Build();
}
public IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
比在App.DataAccess(App.DataAccess/project.json)中修改project.json :
{
"version": "1.0.0-*",
"description": "App.DataAccess Class Library",
"authors": [ "Author" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"frameworks": {
"dnx451": { }
},
"dependencies": {
"EntityFramework.Commands": "7.0.0-beta7",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta7",
},
"commands": {
"ef": "EntityFramework.Commands" // this line is important, it will give you access to 'dnx ef' in command line
}
}
Run Code Online (Sandbox Code Playgroud)
你需要做的就是去App.DataAccess使用dnx ef:
cd App.DataAccess
dnx ef migrations add NewMigration
dnx ef migrations remove
dnx ef database update
dnx ef ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6898 次 |
| 最近记录: |