使用 Entity Framework 5.0.0 RC/EF 5.x DbContext Generator for C#/Visual Studio 2012 RC/.NET 4.0,我试图在我的项目中启用自动迁移。我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
.
但是,下一步,Package Manager Console 命令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" }
// );
//
}
}
}