我有一个EF6驱动的程序,在第一次运行时会在UPDATE语句中崩溃。
我尝试通过数据注释重命名列(我没有测试流体API,因为它应该导致相同的数据库)。我也查看了课程的可见性,但是自上次课程开始以来它们没有改变,因此可以排除。
我已经看了一天中的大部分时间,但找不到错误。数据库看起来很健康,代码编译没有问题。此外,任何update-database
语句都可以无异常地执行。
这是堆栈跟踪:
System.Data.DataException was unhandled by user code
HResult=-2146233087
Message=An exception occurred while initializing the database. See the InnerException for details.
Source=EntityFramework
StackTrace:
bei System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
bei System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
bei System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
bei System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
bei System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
bei System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
bei System.Data.Entity.Internal.InternalContext.Initialize()
bei System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
bei System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
bei System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path)
bei System.Data.Entity.Infrastructure.DbQuery`1.Include(String path)
bei System.Data.Entity.QueryableExtensions.Include[T](IQueryable`1 source, String path)
bei System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable`1 source, Expression`1 path)
bei RpgTools.Characters.CharacterRepository.FindAll() in e:\Users\Robert\Documents\Visual Studio 2013\Projects\RpgTools\Modules\Characters\CharacterRepository.cs:Zeile 88.
bei RpgTools.CharacterPresenter.ViewModels.CharactersViewModel.LoadCharacters() in e:\Users\Robert\Documents\Visual Studio 2013\Projects\RpgTools\Modules\CharacterPresenter\ViewModels\CharactersViewModel.cs:Zeile 169.
InnerException: System.Data.Entity.Infrastructure.DbUpdateException
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
bei System.Data.Entity.Internal.InternalContext.SaveChanges()
bei System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
bei System.Data.Entity.DbContext.SaveChanges()
bei System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
bei System.Data.Entity.Migrations.Infrastructure.MigratorBase.SeedDatabase()
bei System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
bei System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
bei System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
bei System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
bei System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
bei System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
bei System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
bei System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update()
bei System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context)
bei System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
bei System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
InnerException: System.Data.Entity.Core.UpdateException
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
bei System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
bei System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
bei System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
bei System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
bei System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
bei System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
bei System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
bei System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
bei System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
bei System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
bei System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
bei System.Data.Entity.Internal.InternalContext.SaveChanges()
InnerException: System.Data.SqlClient.SqlException
HResult=-2146232060
Message=The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Characters.Characters_Characters.Appearances_AppearanceId". The conflict occurred in database "RpgTools", table "Characters.Appearances", column 'Id'.
The statement has been terminated.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=547
Procedure=""
Server=(LocalDb)\mssqllocaldb
State=0
StackTrace:
bei System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
bei System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
bei System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
bei System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
bei System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
bei System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
bei System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
bei System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
bei System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
bei System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
bei System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
bei System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
bei System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
bei System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
bei System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
InnerException:
这是抛出异常的方法。 include语句抛出异常。我可以补充一点,以下两种方法的类是internal sealed
/// <inheritdoc />
public IDictionaryRange<Guid, Character> FindAll()
{
IDataContainer<ICollection<CharacterItem>> data = this.CreateContainer<ICollection<CharacterItem>>(
this.Characters.Include(c => c.Appearance).Include(c => c.Metadata).ToList(),
this.Culture);
return this.bulkReadConverter.Convert(data);
}
private IDataContainer<TData> CreateContainer<TData>(TData data, CultureInfo culture = null, DateTimeOffset? date = null)
{
return new DataContainer<TData>
{
Content = data,
Culture = culture,
Date = date
};
}
这些是代表我的数据的类:
[Table("Character")]
internal class CharacterItem
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
public string Nickname { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Biography { get; set; }
public string Motto { get; set; }
public int Age { get; set; }
public Guid PortraitsId { get; set; }
public PortraitItem Portrait { get; set; }
public Guid MetadataId { get; set; }
public CharacterMetadataItem Metadata { get; set; }
public Guid AppearancesId { get; set; }
public AppearanceItem Appearance { get; set; }
public Guid OriginId { get; set; }
}
[Table("Appearances")]
internal sealed class AppearanceItem
{
[Key]
public Guid Id { get; set; }
public Genders Gender { get; set; }
public int Height { get; set; }
public int Weight { get; set; }
public string SkinColour { get; set; }
public string EyeColour { get; set; }
public string SpecialFeatures { get; set; }
public string HairColour { get; set; }
public string LipColour { get; set; }
}
以下是数据库架构的SQL语句。
CREATE TABLE [Characters].[Character] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[Name] NVARCHAR (MAX) NULL,
[Nickname] NVARCHAR (MAX) NULL,
[Title] NVARCHAR (MAX) NULL,
[Description] NVARCHAR (MAX) NULL,
[Biography] NVARCHAR (MAX) NULL,
[Motto] NVARCHAR (MAX) NULL,
[Age] INT NOT NULL,
[PortraitsId] UNIQUEIDENTIFIER NOT NULL,
[MetadataId] UNIQUEIDENTIFIER NOT NULL,
[AppearancesId] UNIQUEIDENTIFIER NOT NULL,
[OriginId] UNIQUEIDENTIFIER NOT NULL,
[Appearance_Id] UNIQUEIDENTIFIER NULL,
[Portrait_Id] UNIQUEIDENTIFIER NULL,
CONSTRAINT [PK_Characters.Character] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Characters.Character_Characters.Appearances_Appearance_Id] FOREIGN KEY ([Appearance_Id]) REFERENCES [Characters].[Appearances] ([Id]),
CONSTRAINT [FK_Characters.Character_Characters.Metadata_MetadataId] FOREIGN KEY ([MetadataId]) REFERENCES [Characters].[Metadata] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_Characters.Character_Characters.Portraits_Portrait_Id] FOREIGN KEY ([Portrait_Id]) REFERENCES [Characters].[Portraits] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_MetadataId]
ON [Characters].[Character]([MetadataId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Appearance_Id]
ON [Characters].[Character]([Appearance_Id] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Portrait_Id]
ON [Characters].[Character]([Portrait_Id] ASC);
CREATE TABLE [Characters].[Appearances] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[Gender] INT NOT NULL,
[Height] INT NOT NULL,
[Weight] INT NOT NULL,
[SkinColour] NVARCHAR (MAX) NULL,
[EyeColour] NVARCHAR (MAX) NULL,
[SpecialFeatures] NVARCHAR (MAX) NULL,
[HairColour] NVARCHAR (MAX) NULL,
[LipColour] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_Characters.Appearances] PRIMARY KEY CLUSTERED ([Id] ASC)
);
根据John Castleman的建议,我改变了Schema初始化,这是之前由FluidAPI对DataAnnotations进行的。然后我添加了另一个迁移并应用了另一个update-database
现在我在运行命令时遇到错误,而不仅仅是在启动程序时。
以下是错误和我的迁移:
这是启用了-verbose开关的堆栈跟踪。我不得不删除旧的,因为我达到了字符限制。
Using StartUp project 'Main'.
Using NuGet project 'Characters'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'RpgTools' (DataSource: (LocalDb)\mssqllocaldb, Provider: System.Data.SqlClient, Origin: Configuration).
No pending explicit migrations.
Running Seed method.
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Characters.Character_Characters.Metadata_MetadataId". The conflict occurred in database "RpgTools", table "Characters.Metadata", column 'Id'.
The statement has been terminated.
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.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, 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.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
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)
An error occurred while updating the entries. See the inner exception for details.
迁移:
public partial class v0_1_0 : DbMigration
{
public override void Up()
{
CreateTable(
"Characters.Character",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(),
Nickname = c.String(),
Title = c.String(),
Description = c.String(),
Biography = c.String(),
Motto = c.String(),
Age = c.Int(nullable: false),
PortraitsId = c.Guid(nullable: false),
MetadataId = c.Guid(nullable: false),
AppearancesId = c.Guid(nullable: false),
OriginId = c.Guid(nullable: false),
Appearance_Id = c.Guid(),
Portrait_Id = c.Guid(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("Characters.Appearances", t => t.Appearance_Id)
.ForeignKey("Characters.Metadata", t => t.MetadataId, cascadeDelete: true)
.ForeignKey("Characters.Portraits", t => t.Portrait_Id)
.Index(t => t.MetadataId)
.Index(t => t.Appearance_Id)
.Index(t => t.Portrait_Id);
CreateTable(
"Characters.Appearances",
c => new
{
Id = c.Guid(nullable: false),
Gender = c.Int(nullable: false),
Height = c.Int(nullable: false),
Weight = c.Int(nullable: false),
SkinColour = c.String(),
EyeColour = c.String(),
SpecialFeatures = c.String(),
HairColour = c.String(),
LipColour = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"Characters.Metadata",
c => new
{
Id = c.Guid(nullable: false),
Tags = c.String(),
VoiceActor = c.String(),
Occurrences = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"Characters.Portraits",
c => new
{
Id = c.Guid(nullable: false),
Data = c.Binary(),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropForeignKey("Characters.Character", "Portrait_Id", "Characters.Portraits");
DropForeignKey("Characters.Character", "MetadataId", "Characters.Metadata");
DropForeignKey("Characters.Character", "Appearance_Id", "Characters.Appearances");
DropIndex("Characters.Character", new[] { "Portrait_Id" });
DropIndex("Characters.Character", new[] { "Appearance_Id" });
DropIndex("Characters.Character", new[] { "MetadataId" });
DropTable("Characters.Portraits");
DropTable("Characters.Metadata");
DropTable("Characters.Appearances");
DropTable("Characters.Character");
}
}
public partial class v0_1_1 : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
有趣的是:即使第二次迁移为空,update-database
命令也会失败。第一次迁移运行得很好,但是在启动程序时出现错误。
这里的附加信息是我的存储库的构造函数和它的配置文件。
internal CharacterRepository(IConverter<CharacterItem, Character> characterReadConverter, IConverter<Character, CharacterItem> characterWriteConverter)
: base("name=RpgTools")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<CharacterRepository, Configuration>(true));
this.Characters = this.Set<CharacterItem>();
this.readConverter = new DataConverter<CharacterItem, Character>(characterReadConverter);
this.bulkReadConverter = new DictionaryRangeConverter<CharacterItem, Guid, Character>(characterReadConverter, c => c.Id);
this.writeConverter = new DataConverter<Character, CharacterItem>(characterWriteConverter);
}
internal sealed class Configuration : DbMigrationsConfiguration<CharacterRepository>
{
public Configuration()
{
this.AutomaticMigrationsEnabled = false;
}
protected override void Seed(CharacterRepository context)
{
Guid[] ids =
{
new Guid("13cbbdc8-d2e6-4a9a-9525-fa9043a53082"),
new Guid("3a34fd9b-a6e3-4567-af83-840ddf4417ba"),
new Guid("b1256f9f-8a35-416a-8f22-6be91ffb4041"),
new Guid("2e24283d-1ba0-4cc7-b49c-975937e8512c"),
new Guid("2f66d513-1839-4a26-be7e-39243d6807cf")
};
CharacterItem char1 = new CharacterItem
{
Age = 22,
Appearance = new AppearanceItem
{
EyeColour = "Green",
Gender = Genders.Female,
HairColour = "Red",
Height = 188,
Id = ids[1],
LipColour = "red",
SkinColour = "fair",
Weight = 76
},
Id = ids[0],
Metadata = new CharacterMetadataItem
{
Id = ids[2],
Occurrences = string.Empty,
Tags = "Female; Protagonist"
},
Motto = null,
Name = "Sarah Fenix",
Nickname = string.Empty,
Portrait = null,
Description = "Ravia Hagen's spouse",
Title = string.Empty
};
var char2 = new CharacterItem
{
Age = 22,
Appearance = new AppearanceItem
{
EyeColour = "Green",
Gender = Genders.Female,
HairColour = "Red",
Height = 188,
Id = ids[4],
LipColour = "red",
SkinColour = "fair",
Weight = 76
},
Id = ids[3],
Metadata = new CharacterMetadataItem
{
Id = ids[5],
Occurrences = string.Empty,
Tags = "Female; Protagonist",
VoiceActor = string.Empty
},
Motto = null,
Name = "Ravia Hagen",
Nickname = string.Empty,
Portrait = null,
Description = "Sarah Fenix's spouse",
Title = string.Empty
};
context.Characters.AddOrUpdate(c => c.Id, char1, char2);
}
}
答案 0 :(得分:2)
问题不在于更新本身,而在于您尝试更新的记录会破坏外键约束。
UPDATE语句与FOREIGN KEY约束冲突 “FK_Characters.Characters_Characters.Appearances_AppearanceId”。该 数据库“RpgTools”表中发生冲突 “Characters.Appearances”,列'Id'。
我们没有您的数据库架构,所以您必须搜索您的数据库以找出有关的FK_Characters.Characters_Characters.Appearances_AppearanceId
是什么,然后确保您尝试更新的记录与FK约束“合法”
答案 1 :(得分:0)
尝试[Table("Character", Schema = "Characters")]
或仅[Table("Characters.Character")]
也可以。否则,EF正在尝试使用[dbo].[Character]
。
如果最近引入了架构差异,则可能是问题的根源。
根据问题更新进行修改:
不确定你是如何通过编译器抛出ArrayOutOfBoundsException
- 有时EF隐式空联合? - 但考虑到你的InnerException与Metadata.Id
有关,我认为它怀疑你在数组末尾使用一个值来初始化这个属性(数组5表示索引0 - 4,所以你的ids[5]
超出范围。)
你最初发布时可能会有一些略有不同的顺序,因为它是Appearances.Id是错误的。
答案 2 :(得分:0)
我有类似的问题,但我想出了。我试图将一个具有一些属性的对象插入Table_1中,其中一个是FOREIGN KEY
,它引用此数据库中另一个表(Table_2)中的PRIMARY KEY
。
但是另一个Table_2是空的。因此,我必须使用主键在Table_2中创建一行(一个对象)。 PRIMARY KEY
必须等于FOREIGN KEY
或我想插入到Table_1中的对象的属性。
因为要在表中插入一些具有另一个表外键的对象,SQL Server会检查另一个表是否具有该外键的对象。如果没有,那就什么也没有发生。
答案 3 :(得分:-1)
这似乎是一个数据问题:
Message = UPDATE语句与FOREIGN KEY冲突 约束 &#34; FK_Characters.Characters_Characters.Appearances_AppearanceId&#34 ;.该 数据库&#34; RpgTools&#34;,表中发生冲突 &#34; Characters.Appearances&#34;,column&#39; Id&#39;。