C# 实体框架 4.3 迁移错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9063464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework 4.3 migrations error
提问by Matt Roberts
I've just installed EF 4.3-beta1 for the migrations goodness, and I can't get it working. The error I get:
我刚刚安装了 EF 4.3-beta1 以实现迁移优势,但我无法让它工作。我得到的错误:
PM> Update-Database -Verbose
Using NuGet project 'Project.Domain'.
Using StartUp project 'ProjectWebSite'.
System.InvalidOperationException: No migrations configuration type was found in the assembly 'Project.Domain'.
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
No migrations configuration type was found in the assembly 'Project.Domain'.
I've added a new column to 2 EF classes:
我在 2 个 EF 类中添加了一个新列:
public class MasterInstance
{
public int MasterInstanceId { get; set; }
[Required] public string HostName { get; set; }
[Required] public string Name { get; set; } /* <-- THIS IS NEW */
[Required] public string ConnectionString { get; set; }
public virtual ICollection<MasterInstanceLocation> MasterInstanceLocations { get; set; }
}
And my DbContext looks like this:
我的 DbContext 看起来像这样:
public class ProjectDontext: DbContext, IProjectContext
{
public IDbSet<Installer> Installers { get; set; }
public IDbSet<MasterInstance> MasterInstances { get; set; }
public IDbSet<MasterInstanceLocation> MasterInstanceLocations { get; set; }
}
Any ideas? My EF classes & context live in a separate assembly (Project.Domain). I've tried running the update-database in the context of both the main website and the domain project, and I get the same error either way.
有任何想法吗?我的 EF 类和上下文位于单独的程序集 (Project.Domain) 中。我已经尝试在主网站和域项目的上下文中运行更新数据库,但无论哪种方式,我都会遇到相同的错误。
-- EDIT--
--编辑--
Solution found. It turns out, that you need to enable migrations for your project. You can do this by running Enable-Migrationsin the NuGet console (make sure you have the right project selected - for me this was the project.domain project).
找到解决方案。事实证明,您需要为您的项目启用迁移。您可以通过Enable-Migrations在 NuGet 控制台中运行来完成此操作(确保您选择了正确的项目 - 对我来说这是 project.domain 项目)。
This walkthroughprovides more information
本演练提供了更多信息
采纳答案by Matt Roberts
Solution found. It turns out, that you need to enable migrations for your project. You can do this by running Enable-Migrationsin the NuGet console (make sure you have the right project selected - for me this was the project.domain project).
找到解决方案。事实证明,您需要为您的项目启用迁移。您可以通过Enable-Migrations在 NuGet 控制台中运行来完成此操作(确保您选择了正确的项目 - 对我来说这是 project.domain 项目)。
This walkthroughprovides more information
本演练提供了更多信息
回答by Anjani
Sometimes, even if you have enabled migration, this problem can occur. It means that configuration file has been deleted. In this case, you can run
有时,即使您启用了迁移,也可能会出现此问题。这意味着配置文件已被删除。在这种情况下,您可以运行
Enable-Migrations -Force
in the Package Manager Console. -Forceparameter is to override migration configuration file.
在包管理器控制台中。-Force参数是覆盖迁移配置文件。
回答by WWC
If you had already enabled migrations and just started seeing this error after some windows updates, ensure you are using the same version of Entity Framework across all projects using the NuGet Package Manager.
如果您已启用迁移,并且在某些 Windows 更新后才开始看到此错误,请确保您使用 NuGet 包管理器在所有项目中使用相同版本的实体框架。
Recent windows updates may have installed a newer version of Entity Framework into your active project.
最近的 Windows 更新可能已将较新版本的实体框架安装到您的活动项目中。
Background: Around 16 Mar 2016, I started getting the "no migrations configuration type" error when trying to add migrations to a project where I had already enabled migrations and had successfully done migrations before.
背景:大约在 2016 年 3 月 16 日,当我尝试将迁移添加到我已经启用迁移并且之前成功完成迁移的项目时,我开始收到“无迁移配置类型”错误。
I noticed that around March 10, a new stable version of Entity Framework 6 had been released.
我注意到 3 月 10 日左右,实体框架 6 的新稳定版本已经发布。
If I specified the -ContextTypeName parameter in the enable-migrations command, I got an error indicating the migrations were already enabled.
如果我在 enable-migrations 命令中指定了 -ContextTypeName 参数,则会收到一条错误消息,表明迁移已启用。
Another error I got as I was troubleshooting indicated that the Configuration type was not inheriting from the System.Data.Entity.ModelConfiguration.EntityTypeConfiguration, even though it was.
我在故障排除时遇到的另一个错误表明配置类型不是从 System.Data.Entity.ModelConfiguration.EntityTypeConfiguration 继承的,即使它是。
That led me to believe different versions of the Entity Framework were conflicting.
这让我相信不同版本的实体框架存在冲突。
Resolution:
解析度:
1) Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution
1) 工具 -> Nuget 包管理器 -> 管理解决方案的 Nuget 包
2) (Not sure if this step is necessary, but..) I updated my version of the Nuget Package Manager to the latest version. Also, after updating my version of Nuget Package Manager, I had to restart Visual Studio twice before the NuGet Command line would work properly.
2)(不确定这一步是否必要,但是..)我将我的 Nuget 包管理器版本更新到了最新版本。此外,在更新我的 Nuget 包管理器版本后,我必须重新启动 Visual Studio 两次,NuGet 命令行才能正常工作。
3) Tools -> Nuget package Manager -> Manage Nuget Packages for Solution -> Search Installed packages -> Type Entity Framework
3) 工具 -> Nuget 包管理器 -> 管理解决方案的 Nuget 包 -> 搜索已安装的包 -> 类型实体框架
a. You may see more than one version of Entity Framework there.
一种。您可能会在那里看到多个版本的实体框架。
b. Click Manage on each version of Entity Framework and ensure that your projects are using the SAME version of Entity Framework. ?Uncheck the version of Entity Framework that you are not using and for the version of Entity Framework you ARE using make sure it is checked across your projects that need it.
湾 单击每个版本的实体框架上的管理,并确保您的项目使用相同版本的实体框架。?取消选中您没有使用的实体框架版本,对于您正在使用的实体框架版本,请确保在需要它的项目中进行检查。
Again, as noted in step 2, I had to restart visual studio twice to get the NuGet Package Manager Console to work properly after updating my version of the NuGet Package Manager. I got an error starting the console the first time, and "exception calling createinstancefrom with 8 arguments could not load file or assembly EntityFramework" when running the enable-migrations command the second time.
同样,如步骤 2 中所述,在更新我的 NuGet 包管理器版本后,我必须重新启动 Visual Studio 两次才能使 NuGet 包管理器控制台正常工作。我第一次启动控制台时出错,第二次运行 enable-migrations 命令时“异常调用 createinstancefrom 带有 8 个参数无法加载文件或程序集 EntityFramework”。
Restarting visual studio seemed to resolve those issues, however.
然而,重新启动 Visual Studio 似乎解决了这些问题。
回答by James L.
For me, this error occurred because I had the wrong project selected in the Package Manager Console's "Default Project" in VS2019.
对我来说,发生这个错误是因为我在 VS2019 的包管理器控制台的“默认项目”中选择了错误的项目。
回答by B--rian
Even when migration are enabled, the described behavior can occur if an incorrect Default Projecthas been chosen in the drop-down menu of the Package Manager Console. A non-graphical way around is to expand the command you are using, and specify the correct project name with the parameter -ProjectName
即使启用了迁移,如果在Package Manager Console的下拉菜单中选择了不正确的默认项目,也可能会发生所描述的行为。一种非图形化的方法是展开您正在使用的命令,并使用参数指定正确的项目名称-ProjectName
Update-Database -Verbose -ProjectName TheCorrectProjectName
You might still get a warning like
您可能仍会收到类似警告
Cannot determine a valid start-up project. Using project 'TheCorrectProjectName' instead. Your configuration file and working directory may not be set as expected. Use the -StartUpProjectName parameter to set one explicitly.
无法确定有效的启动项目。改用项目“TheCorrectProjectName”。您的配置文件和工作目录可能未按预期设置。使用 -StartUpProjectName 参数明确设置一个。
Nevertheless, this additional command line parameter solved the issue for me.
尽管如此,这个额外的命令行参数为我解决了这个问题。

![将数组或字符串 [] 的 C# 字符串分配给 javascript 数组](/res/img/loading.gif)