C# 实体框架5代码优先。如何指定要删除的表?

C# 实体框架5代码优先。如何指定要删除的表?,c#,ef-code-first,entity-framework-5,C#,Ef Code First,Entity Framework 5,我想删除一些表格,然后按照我的模型设计再次创建。但是CodeFirst从来没有这样做过,一直试图应用我所做的更改,但由于某些原因他不能。 models类的设计是可以的,但SQL上的表不是,这就是为什么我们希望删除并重新创建 我对模型做了一些修改,特别是使用了键和外键。 所以为什么我认为如果我可以设置我想要删除哪些表,代码优先将理解我的新实现 Erro输出更新数据库 PM> update-database -verbose -force Using StartUp project 'WebT

我想删除一些表格,然后按照我的模型设计再次创建。但是CodeFirst从来没有这样做过,一直试图应用我所做的更改,但由于某些原因他不能。 models类的设计是可以的,但SQL上的表不是,这就是为什么我们希望删除并重新创建

我对模型做了一些修改,特别是使用了键和外键。 所以为什么我认为如果我可以设置我想要删除哪些表,代码优先将理解我的新实现

Erro输出更新数据库

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.
PM>更新数据库-verbose-force
使用启动项目“WebTier”。
使用NuGet项目“Target.Data.SqlServer”。
指定'-Verbose'标志以查看应用于目标数据库的SQL语句。
System.InvalidOperationException:无法确定类型“Target.Data.Model.TacticalGoal”和“Target.Data.Model.ICB”之间关联的主端。必须使用关系fluent API或数据注释显式配置此关联的主体端。
位于System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.GenerateIndependentAssociationType(EdmAssociationType associationType,DbDatabaseMappingDatabaseMapping)
位于System.Data.Entity.ModelConfiguration.Edm.Services.AssociationTypeMappingGenerator.Generate(EdmAssociationType associationType,DbDatabaseMappingDatabaseMapping)
位于System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateAsociationTypes(EdmModel模型,DBDatabaseMappingDatabaseMapping数据库映射)
位于System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel模型)
位于System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
位于System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
位于System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
位于System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput输入)
在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()中
在System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()中
位于System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext上下文,XmlWriter编写器)
在System.Data.Entity.Migrations.Extensions.DbContextExtensions.c_uuDisplayClass1.b_uu0(XmlWriter w)中
位于System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(操作'1 writeXml)
位于System.Data.Entity.Migrations.Extensions.DbContextensions.GetModel(DbContext上下文)
位于System.Data.Entity.Migrations.DbMigrator..ctor(dbmigrations配置,DbContext用户上下文)
位于System.Data.Entity.Migrations.DbMigrator..ctor(dbmigrations配置)
位于System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()处
在System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()中
位于System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()处
无法确定类型“Target.Data.Model.TacticalGoal”和“Target.Data.Model.ICB”之间关联的主端。必须使用关系fluent API或数据注释显式配置此关联的主体端。

您可以尝试降级迁移。回到您过去在“创建表”迁移之前创建的迁移,将解决您的问题。您可以在Package Manager控制台中编写此命令:

Update-Database –TargetMigration: yourPreviousMigrationName

之后,您可以使用新表单再次创建表。

在我的情况下,它看起来很有效,只是EF将尝试应用sames迁移。有一种方法可以删除或删除迁移?返回到上一次迁移后,请转到“解决方案爆炸器”->“迁移”文件夹,尝试右键单击并删除当前迁移以外的最新迁移。那么它就不能应用相同的迁移了。那么自动迁移呢?这些不在其中,代码首先要安装它们。您可以在中了解有关使用自动迁移的详细信息: