EF 6 启用迁移找不到上下文
EF 6 Enable-Migrations Can't Find Context
我正在尝试在 EF 6.1.3 - .NET 4.5 中设置 EF 代码优先迁移。
我的解决方案中有多个项目,启动项目是 Songbirds.Web
。我创建了一个名为 Songbirds.Dal.EntityFramework
的项目来包含我的存储库、数据库上下文和迁移。
我创建了我的上下文 class:
namespace Songbirds.Dal.EntityFramework
{
public class SongbirdsDbContext : IdentityDbContext<ApplicationUser>, IUnitOfWork
{
public SongbirdsDbContext()
: this("name=SongbirdsDBContext")
{
}
...
}
}
整个解决方案构建正确,没有错误。
我进入项目管理器控制台并将默认项目设置为 Songbirds.Dal.EntityFramework
和 运行 enable-migrations
命令,我收到以下错误:
PM> enable-migrations
No context type was found in the assembly 'Songbirds.Dal.EntityFramework'.
我尝试明确指定上下文类型,结果如下:
PM> enable-migrations -ContextTypeName Songbirds.Dal.EntityFramework.SongbirdsDbContext
The context type 'Songbirds.Dal.EntityFramework.SongbirdsDbContext' was not found in the assembly 'Songbirds.Dal.EntityFramework'.
SongbirdsDbContext 是 Songbirds.Dal.EntityFramework 项目的一部分。知道我做错了什么以及为什么它无法识别上下文吗?
确保将默认项目设置为具有 EF 上下文的项目。
试试 运行
enable-migrations -ContextTypeName Songbirds.Dal.EntityFramework
将 .songBirdsContext 添加到末尾可能是问题所在。
我想我通过反复试验找到了答案。
我首先将上下文 class 更改为继承自 DbContext class 而不是 IdentifyDbContext:
public class SongbirdsDbContext : DbContext
和re-ranenable-migrations
命令发现如下错误:
Could not load file or assembly 'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
添加对所需程序集的适当引用后,我能够成功启用迁移。我不确定为什么从 DbContext 继承显示此错误,而从 IdentityDbContext 继承却没有。
我发现我这边的解决方案是我刚刚创建了一个项目,但我还没有构建它。所以构建我的项目然后重新运行命令有效
我正在尝试在 EF 6.1.3 - .NET 4.5 中设置 EF 代码优先迁移。
我的解决方案中有多个项目,启动项目是 Songbirds.Web
。我创建了一个名为 Songbirds.Dal.EntityFramework
的项目来包含我的存储库、数据库上下文和迁移。
我创建了我的上下文 class:
namespace Songbirds.Dal.EntityFramework
{
public class SongbirdsDbContext : IdentityDbContext<ApplicationUser>, IUnitOfWork
{
public SongbirdsDbContext()
: this("name=SongbirdsDBContext")
{
}
...
}
}
整个解决方案构建正确,没有错误。
我进入项目管理器控制台并将默认项目设置为 Songbirds.Dal.EntityFramework
和 运行 enable-migrations
命令,我收到以下错误:
PM> enable-migrations
No context type was found in the assembly 'Songbirds.Dal.EntityFramework'.
我尝试明确指定上下文类型,结果如下:
PM> enable-migrations -ContextTypeName Songbirds.Dal.EntityFramework.SongbirdsDbContext
The context type 'Songbirds.Dal.EntityFramework.SongbirdsDbContext' was not found in the assembly 'Songbirds.Dal.EntityFramework'.
SongbirdsDbContext 是 Songbirds.Dal.EntityFramework 项目的一部分。知道我做错了什么以及为什么它无法识别上下文吗?
确保将默认项目设置为具有 EF 上下文的项目。
试试 运行
enable-migrations -ContextTypeName Songbirds.Dal.EntityFramework
将 .songBirdsContext 添加到末尾可能是问题所在。
我想我通过反复试验找到了答案。 我首先将上下文 class 更改为继承自 DbContext class 而不是 IdentifyDbContext:
public class SongbirdsDbContext : DbContext
和re-ranenable-migrations
命令发现如下错误:
Could not load file or assembly 'Microsoft.AspNet.Identity.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
添加对所需程序集的适当引用后,我能够成功启用迁移。我不确定为什么从 DbContext 继承显示此错误,而从 IdentityDbContext 继承却没有。
我发现我这边的解决方案是我刚刚创建了一个项目,但我还没有构建它。所以构建我的项目然后重新运行命令有效