gpt4 book ai didi

entity-framework - Entity Framework外键代码第一次迁移错误

转载 作者:行者123 更新时间:2023-12-04 08:30:46 29 4
gpt4 key购买 nike

我有一个 Entity Framework 类,其中有两个指向另一个对象的引用

public class Review
{
[Key]
public int Id
{get;set;}

public int? FeatureId {get;set;}

public Feature Feature {get;set;}
}

public class Feature
{
[Key]
public int Id
{get;set;}

public ICollection<Review> Reviews {get;set;}

}

当我使用 Add-Migration

生成迁移时,这工作正常

现在,我添加了第二个条目

public class Review
{
[Key]
public int Id
{get;set;}

public int? FeatureId {get;set;}

public Feature Feature {get;set;}

public int? CompareToFeatureId {get;set;}

public Feature CompareToFeature {get;set;}
}

当我运行迁移时,它会做一些奇怪的事情,比如重命名原始列。

   RenameColumn(table: "dbo.Reviews", name: "FeatureId", newName: "Feature_Id");
AddColumn("dbo.Reviews", "CompareToFeatureId", c => c.Int());
AddForeignKey("dbo.Reviews", "FeatureId", "dbo.Features", "Id");
AddForeignKey("dbo.Reviews", "CompareToFeatureId", "dbo.Features", "Id");
CreateIndex("dbo.Reviews", "FeatureId");
CreateIndex("dbo.Reviews", "CompareToFeatureId");

那很好,但是当我运行 update-database 时迁移失败了

Applying code-based migrations: [201211092218271_Test2].
Applying code-based migration: 201211092218271_Test2.
System.Data.SqlClient.SqlException (0x80131904): Foreign key 'FK_dbo.Reviews_dbo.Features_FeatureId' references invalid column 'FeatureId' in referencing table 'Reviews'.
Could not create constraint. See previous errors.
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)
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.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
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, XDocument targetModel, IEnumerable`1 operations, 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.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ClientConnectionId:f1573dba-4658-462f-969a-9afbf1e4374a
Foreign key 'FK_dbo.Reviews_dbo.Features_FeatureId' references invalid column 'FeatureId' in referencing table 'Reviews'.
Could not create constraint. See previous errors.

不确定在这里做什么。有什么建议吗?

最佳答案

由于您没有遵循约定 (Feature_ID),您需要稍微帮助一下:在 FeatureId 属性上放置一个属性 [ForeignKey("Feature")]

[ForeignKey("Feature")]
public int? FeatureId { get; set; }

public Feature Feature { get; set; }

关于entity-framework - Entity Framework外键代码第一次迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13316899/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com