Windows Azure迁移错误

时间:2012-12-13 13:46:26

标签: azure asp.net-mvc-4 visual-studio-2012

我正在遵循使用带有sql数据库的asp.net的azure的教程,并且很难尝试通过我收到的错误。我关注的教程是 - “http://www.windowsazure.com/en-us/develop/net/tutorials/web-site-with-sql-database/#aspnetwindowsazureinfo”

在软件包管理器控制台中我输入了“enable-migrations -ContextTypeName ToDoListApp.Models.ToDoDb”并且工作正常,如下图所示输出。

Checking if the context targets an existing database...
Code First Migrations enabled for project ToDoListApp.

然后我输入“add-migration Initial”,我收到了这个错误..

“无法构建下一次迁移,因为目标数据库是使用早于EF 4.3的Code First版本创建的,并且不包含迁移历史记录表。要开始对此数据库使用迁移,请确保当前模型与目标数据库并执行迁移更新过程。(在Visual Studio中,您可以使用Package Manager控制台中的Update-Database命令执行迁移更新过程。)

我还使用了命令“-Verbose” - 并发现错误在目标数据库中。

Using StartUp project 'ToDoListApp'.
Using NuGet project 'ToDoListApp'.
Target database is: 'ToDoListApp.Models.ToDoDb' (DataSource: .\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention). 
  • 注意:*我已经在azure上建立了一个网站,以及sql数据库,并通过发布将其链接到这个项目。

我已将错误输入Google,有人说“Add-Migration InitialMigration -IgnoreChanges”可以修复它,

“http://www.woohoo.uk.com/Blog/updating-entity-framework-4-1-with-code-first-migrations”

和其他人说只是在包管理器控制台中更新数据库,这似乎不起作用,我正在按照Microsoft提供的教程,以及我收到此问题的方式。

解决这种情况的任何帮助都将不胜感激,我使用visual studio 2012,c#asp.net mv3 framework 4.5。

在我的web.config文件中,我也输入了代码

 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  <contexts>
      <context type="ToDoListApp.Models.ToDoDb, ToDoListApp">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion">
        <parameters>
          <parameter value="ToDoDb_DatabasePublish"/>
        </parameters>
        </databaseInitializer>
      </context>
    </contexts>
  </entityFramework>

我输入了“Add-Migration InitialMigration -IgnoreChanges”

输出的错误是..

 System.InvalidOperationException: Failed to set database initializer of type                 'System.Data.Entity.MigrateDatabaseToLatestVersion' for DbContext type 'ToDoListApp.Models.ToDoDb, ToDoListApp' specified in the application configuration. See inner exception for details. ---> System.TypeLoadException: Could not load type 'System.Data.Entity.MigrateDatabaseToLatestVersion' from assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase,  Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName, Boolean throwOnError)
   at System.Data.Entity.Internal.ConfigFile.DatabaseInitializerElement.GetInitializerType()
   at System.Data.Entity.Internal.AppConfig.InternalApplyInitializers(Boolean force)
   --- End of inner exception stack trace ---
   at System.Data.Entity.Internal.AppConfig.InternalApplyInitializers(Boolean force)
   at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
   at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
   at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at System.Data.Entity.Migrations.History.HistoryRepository.Exists[TContext](HistoryContextBase`1 context)
   at System.Data.Entity.Migrations.History.HistoryRepository.GetPendingMigrations(IEnumerable`1 localMigrations)
   at System.Data.Entity.Migrations.DbMigrator.GetPendingMigrations()
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.GetPendingMigrations()
   at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Failed to set database initializer of type 'System.Data.Entity.MigrateDatabaseToLatestVersion' for DbContext type 'ToDoListApp.Models.ToDoDb, ToDoListApp' specified in the application configuration. See inner exception for details.

1 个答案:

答案 0 :(得分:1)

已修复 -

只需要我登录sql server对象资源管理器并删除表下的EdmMetadata,然后在web.config中输入我的连接字符串,这是一个例子,如果有人读过这个并遇到同样的问题..

<add name="DefaultConnection" connectionString="server=tcp:* insert server name*.database.windows.net,1433;Database=* insert database name *;User ID=*insert username *@* insert server name*;Password={*insert password here *};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"  />