从EDMX切换到Code First后的迁移会抛出MetadataException

时间:2015-09-18 08:46:10

标签: entity-framework ef-code-first ef-migrations edmx

我正在将项目从使用EDMX文件更改为Code First。该项目是解决方案中的少数项目之一,它只包含实体和DbContext类。

这是我到目前为止所做的:

  1. 从解决方案中删除了旧项目(但尚未将其删除,只是重命名了该文件夹)。
  2. 在解决方案中添加了一个新的类库项目,并为其指定了与旧版本相同的名称。
  3. 使用NuGet将Entity Framework添加到新项目中。
  4. 将旧EDMX文件生成的所有实体类复制到新项目中并将其全部包含在内。
  5. 也复制了DbContext类。
  6. 修改了app.config文件,将连接字符串更改为没有元数据内容的标准连接字符串。
  7. 添加了对用于使用旧项目的所有其他项目的引用。
  8. 一切看起来都很好并且编译时没有错误,但是当我尝试enable-migrations时会抛出此错误:

    PM> enable-migrations
    Checking if the context targets an existing database...
    System.Data.Entity.Core.MetadataException: Unable to load the specified metadata resource.
       at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
       at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
       at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
       at System.Data.Entity.Core.Metadata.Edm.MetadataCache.SplitPaths(String paths)
       at System.Data.Entity.Core.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0()
       at System.Data.Entity.Core.Common.Utils.Memoizer`2.Result.GetValue()
       at System.Data.Entity.Core.Common.Utils.Memoizer`2.Evaluate(TArg arg)
       at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetArtifactLoader(DbConnectionOptions effectiveConnectionOptions)
       at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetMetadataWorkspace(DbConnectionOptions effectiveConnectionOptions)
       at System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace()
       at System.Data.Entity.Core.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection()
       at System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator translator, ColumnMapFactory columnMapFactory)
       at System.Data.Entity.Internal.InternalConnection.CreateObjectContextFromConnectionModel()
       at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
       at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
       at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
       at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
       at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
       at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
       at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
       at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
       at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
       at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
       at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
       at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
       at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
    Unable to load the specified metadata resource.
    

    看起来它仍然认为我正在使用EDMX。我尝试运行应用程序时也遇到此错误。

    我错过了什么?

1 个答案:

答案 0 :(得分:2)

您必须更改初始项目的连接字符串(asp.net或winforms项目)。