EF 6启用 - 迁移无法查找上下文

时间:2015-12-30 16:20:15

标签: c# entity-framework ef-migrations

我正在尝试在EF 6.1.3 - .NET 4.5中设置EF代码首次迁移。

我的解决方案中有多个项目,启动项目为Songbirds.Web。我创建了一个名为Songbirds.Dal.EntityFramework的项目来包含我的存储库,数据库上下文和迁移。

我创建了我的上下文类:

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'.

我尝试使用以下结果明确指定Context Type:

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项目的一部分。任何想法我做错了什么以及为什么它没有认识到背景?

4 个答案:

答案 0 :(得分:1)

我想我通过反复试验找到了答案。 我首先将上下文类更改为继承自DbContext类而不是IdentifyDbContext:

public class SongbirdsDbContext : DbContext

并重新运行enable-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.

在为所需程序集添加适当的引用后,我能够成功启用迁移。我不确定为什么从继承自IdentityDbContext的DbContext继承显示此错误。

答案 1 :(得分:0)

确保将默认项目设置为具有EF上下文的项目。

答案 2 :(得分:0)

尝试运行

enable-migrations -ContextTypeName Songbirds.Dal.EntityFramework

将.songBirdsContext添加到最后可能是问题。

答案 3 :(得分:0)

我发现这方面的解决方案是我刚刚创建了一个项目,但尚未构建。因此,构建我的项目然后重新运行命令就可以了