【发布时间】:2012-08-04 09:28:46
【问题描述】:
使用 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" }
// );
//
}
}
}
【问题讨论】:
-
您的解决方案中有多个项目吗?顺便提一句。您提到您正在使用 DbContext Generator - 迁移仅适用于代码。
-
@LadislavMrnka 啊,大概是这样,我在做模型首先想到它。我不太了解实体框架,而且我总是首先在视觉上设计模型。我将研究代码优先方法,看看是否必须切换到该方法。我不太明白如果代码是从模型生成的会有什么不同。
-
我不确定这个限制是否没有从 5.0RC 中删除,但是当您尝试迁移不是首先使用代码创建的模型时,以前的版本会引发错误。无论如何,应该稍后抛出该错误 - 您的问题可能与此无关。
-
@LadislavMrnka 是的,关于错误的实际原因的不确定性真的令人沮丧:(顺便说一句,解决方案只是一个项目。
-
@LadislavMrnka 我现在尝试了代码优先的方法,我自己定义了数据库模型和上下文类,我得到了完全相同的错误:(
标签: entity-framework nuget visual-studio-2012 database-migration entity-framework-5