我在我的MVC項目中使用代碼第一個實體框架5。今天,我正在對我的域模型進行一些更改,並更新了其他應用程序以處理這些新更改。自然,當對域模型進行更改時,您需要更新數據庫。我們使用代碼遷移來做到這一點(手動遷移,就是這樣)。但是,當我試圖通過包控制檯添加新遷移時,出現以下異常:實體框架代碼遷移:異常已被調用的目標引發
調用目標引發異常。
我已經嘗試將啓動項目添加到命令,它也沒有工作。我所有的項目都沒有編譯錯誤。
編輯:另外,這種情況無論我做什麼:更新數據庫或更新數據庫與目標的遷移。似乎都給我同樣的例外。
這是調用堆棧:
PM> add-migration MyMigrationName
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context)
at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
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.ForceOSpaceLoadingForKnownEntityTypes()
at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()
at EntityFramework.Audit.AuditLogger..ctor(DbContext dbContext, AuditConfiguration configuration)
at EntityFramework.Extensions.AuditExtensions.BeginAudit(DbContext dbContext, AuditConfiguration configuration)
at Project.DataAccessLogic.MyContext..ctor() in c:..\MyContext.cs:line 125
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at System.Data.Entity.Infrastructure.DbContextInfo.<CreateActivator>b__0()
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo)
at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Exception has been thrown by the target of an invocation.
我不知道我應該在哪裏開始尋找。
您是否將'DbMigrationsConfiguration.AutomaticMigrationsEnabled'設置爲true? – Nilesh
不,我們正在手動應用代碼遷移,以便將參數設置爲false。 – Ruudt
您的數據是否在一個單獨的項目中訪問?如果有的話試試'Add-Migration -StartUpProjectName'你的DAL項目「MyNewMigration' – Thewads