实体框架5.0 RC - 程序包管理器命令'add-migration'由于假定缺少配置类型而失败

时间:2012-08-07 11:14:17

标签: entity-framework nuget visual-studio-2012 database-migration entity-framework-5

为C#/ Visual Studio 2012 RC / .NET 4.0使用Entity Framework 5.0.0 RC / EF 5.x DbContext Generator,我正在尝试在项目中启用自动迁移。我在Package Manager控制台中运行enable-migrations

PM> enable-migrations
No classes deriving from DbContext found in the current project.
Edit the generated Configuration class to specify the context to enable migrations for.
Code First Migrations enabled for project Test.

正如您所看到的,它没有自动检测我的DbContext派生类型,但我通过在生成的代码文件Migrations/Configuration.cs中输入此类型的名称来轻松解决这个问题。

但是,下一步,程序包管理器控制台命令enable-migrations由于未找到上一步添加的迁移配置类型而失败。

PM> add-migration Initial
No migrations configuration type was found in the assembly 'Test'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

我该如何解决这个问题?

编辑:我发现我可以使用参数-ConfigurationTypeName指定配置类型的名称:

PM> add-migration -ConfigurationTypeName Test.Migrations.Configuration Initial
The type 'Configuration' is not a migrations configuration type.

这仍然不起作用,但至少它阐明了add-migration保释的原因,即它认为Test.Migrations.Configuration不是迁移配置类型。有没有人知道为什么它不被接受,因为它是由enable - migrations生成的?请参阅下面生成的代码以供参考(UserModelContainer派生自DbContext):

namespace Test.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Test.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<UserModelContainer>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(UserModelContainer context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

1 个答案:

答案 0 :(得分:9)

问题是我在针对.NET framework 4.5时安装了Entity Framework 5.0.0 RC。由于部署到Windows Azure,我发现我不得不以.NET 4.0为目标。我不知道NuGet的复杂性,但似乎为.NET 4.5安装的EF软件包在我的4.0目标项目中无法正常工作。

重新安装EF NuGet软件包后,在.NET 4.0中定位我的项目时,一切运行良好。