2014-04-04 24 views
0

我想刪除一些表格,並根據我的模型設計再次創建。但CodeFirst從來不會這樣做,不斷嘗試應用我所做的更改,但出於某種原因,他不能。 模型類的設計是好的,但SQL上的表不是,這就是爲什麼,想要再次刪除和重新創建。實體框架5代碼優先。如何指定我想刪除的表格?

我在模型上做了一些更改,特別是使用鍵和外鍵。 那麼,爲什麼我認爲如果我可以設置哪些表,我想放棄,代碼首先會理解我的新實現。

埃羅輸出更新數據庫

PM> update-database -verbose -force 
Using StartUp project 'WebTier'. 
Using NuGet project 'Target.Data.SqlServer'. 
Specify the '-Verbose' flag to view the SQL statements being applied to the target database. 
System.InvalidOperationException: Unable to determine the principal end of an association between the types 'Target.Data.Model.TacticalGoal' and 'Target.Data.Model.ICB'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 
    at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.GenerateIndependentAssociationType(EdmAssociationType associationType, DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.Generate(EdmAssociationType associationType, DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateAssociationTypes(EdmModel model, DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel model) 
    at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) 
    at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) 
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) 
    at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) 
    at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 
    at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel() 
    at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer) 
    at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w) 
    at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml) 
    at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context) 
    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.UpdateRunner.RunCore() 
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 
Unable to determine the principal end of an association between the types 'Target.Data.Model.TacticalGoal' and 'Target.Data.Model.ICB'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 

回答

0

您可以嘗試降級遷移。回到之前在「創建表」遷移之前創建的遷移,將解決您的問題。您可以在Package-Manager控制檯中編寫此命令:

Update-Database –TargetMigration: yourPreviousMigrationName 

之後,您可以使用新窗體重新創建表格。

+0

它看起來在我的情況下工作,除了EF將嘗試應用相同的遷移。有一種方法可以刪除或刪除遷移? – CidaoPapito

+0

回到過去的遷移之後,進入您的Solution Exploler-> Migrations文件夾,嘗試右擊並刪除最新的遷移,而不是您當前的遷移。然後它無法應用相同的遷移。 – kkakkurt

+0

那麼自動遷移呢?這些不在那裏,代碼首先要安裝它們。 – CidaoPapito