我正在创建一个code first
项目。
我有一些实体,"有罪的"以下是:
城市
public class City
{
[Key]
public int CityId { get; set; }
[Required]
public int CAP { get; set; }
[Required]
public string Name { get; set; }
public int ProvinceId { get; set; }
[ForeignKey("ProvinceId")]
public virtual Province Province { get; set; }
public virtual ICollection<Shop> Shops { get; set; }
public virtual ICollection<Street> Streets { get; set; }
}
省
public class Province
{
[Key]
public int ProvinceId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Tag { get; set; }
public int RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual Region Region { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
我有其他实体,但它们不在日志问题中,所以我避免发布它们,如果需要,我会添加。
现在,我成功完成了Add-Migration name
代码。
问题出在Update-Database -verbose
(我添加了-verbose方法来输出)
这是来自-verbose的相关sql代码:
[...]
CREATE TABLE [dbo].[Cities] (
[Id] [int] NOT NULL IDENTITY,
[CAP] [int] NOT NULL,
[Name] [nvarchar](max) NOT NULL,
[ProvinceId] [int] NOT NULL,
CONSTRAINT [PK_dbo.Cities] PRIMARY KEY ([Id])
)
CREATE INDEX [IX_ProvinceId] ON [dbo].[Cities]([ProvinceId])
CREATE TABLE [dbo].[Provinces] (
[Id] [int] NOT NULL IDENTITY,
[Name] [nvarchar](max) NOT NULL,
[Tag] [nvarchar](max) NOT NULL,
[RegionId] [int] NOT NULL,
CONSTRAINT [PK_dbo.Provinces] PRIMARY KEY ([Id])
)
[...]
ALTER TABLE [dbo].[Cities] ADD CONSTRAINT [FK_dbo.Cities_dbo.Provinces_ProvinceId] FOREIGN KEY ([ProvinceId]) REFERENCES [dbo].[Provinces] ([ProvinceId]) ON DELETE CASCADE
现在有错误:
外部密钥&#39; FK_dbo.Cities_dbo.Provinces_ProvinceId&#39;引用无效列&#39; ProvinceId&#39;在表中有参考文献&#39; dbo.Provinces&#39;。无法创建关系,请参阅先前的错误
(我手动翻译,可能不完美)
LOG:
[与上述相同的错误]
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
我检查了我发现的每一个解决方案,但没有人解决我的问题。我更改了外键,它的名称和它的位置。我的表的Id名称,但没有..
我做错了什么?
任何帮助将不胜感激
答案 0 :(得分:1)
查看Update-Database
日志,可以注意到两个表的PK都被命名为Id
(而不是模型隐含的ProvinceId
和CityId
):< / p>
[ Id ] [int] NOT NULL IDENTITY,
这就是外键约束报告无效列名dbo.Provinces.ProvinceId
的原因。
因此,模型和生成的迁移之间必定存在一些不同步的问题。