2014-04-04 27 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