当我尝试运行Seed方法时,我运行以下错误消息。
System.Data.Entity.Infrastructure.DbUpdateException:发生错误 在更新条目时。有关详细信息,请参阅内部异常---> System.Data.Entity.Core.UpdateException:发生错误 更新条目。有关详细信息,请参阅内部异常---> System.Data.SqlClient.SqlException:INSERT语句冲突 使用FOREIGN KEY约束" FK_dbo.Movies_dbo.Genres_GenreId"。 冲突发生在数据库中 " C:\用户\ PATH \ SOURCE \ REPOS \ MVCMOVIE \ MVCMOVIE \ APP_DATA \ MOVIES.MDF&#34 ;, table" dbo.Genres",column' Id'。声明已经终止。
这是我的Configuration.cs文件
namespace MvcMovie.Migrations
{
using MvcMovie.Models;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<MvcMovie.Models.MovieDBContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey = "MvcMovie.Models.MovieDBContext";
}
protected override void Seed(MvcMovie.Models.MovieDBContext context)
{
context.Genres.AddOrUpdate(i => i.Title,
new Genre
{
Title = "Comedy"
},
new Genre
{
Title = "Romantic"
});
context.Ratings.AddOrUpdate(i => i.Title,
new Rating
{
Title = "General"
},
new MvcMovie.Models.Rating
{
Title = "Parentel guidence"
});
context.Movies.AddOrUpdate(i => i.Title,
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-1-11"),
GenreId = 1,
RatingId = 1,
Price = 7.99M
},
new Movie
{
Title = "Ghostbusters ",
ReleaseDate = DateTime.Parse("1984-3-13"),
GenreId = 1,
RatingId = 1,
Price = 8.99M
},
}
);
}
}
}
&#13;
评级和流派在电影表中设置为FK。有没有人之前有这种类型的错误?
该声明已被终止。在System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 布尔值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,布尔&amp; dataReady)at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()at System.Data.SqlClient.SqlDataReader.get_MetaData()at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,String resetOptionsString,Boolean isInternal,Boolean forDescribeParameterEncryption)at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32超时,任务&amp; task,Boolean asyncWrite,Boolean inRetry, SqlDataReader ds,Boolean describeParameterEncryptionRequest)at System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
1 c)at System.Data.Entity.Infrastructure.Interception.InternalDispatcher
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func
3操作,TInterceptionContext interceptionContext, 行动3 executing, Action
3已执行) System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(的DbCommand 命令,DbCommandInterceptionContext interceptionContext)at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(的CommandBehavior 行为) System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
在 System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary2 identifierValues, List
1 generatedValues)at at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() ---内部异常堆栈跟踪结束---在System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() 在 System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.b__2(UpdateTranslator ut)at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update [T](T noChangesResult,Func2 updateFunction) at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
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&LT;&GT; c__DisplayClass2a.b__27() 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func键
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 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)
在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations,String targetMigrationId,String lastMigrationId)
在System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) System.Data.Entity.Migrations.DbMigrator&LT;&GT; c__DisplayClasse.b__d() 在 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(动作 mustSucceedToKeepDatabase)at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(动作 mustSucceedToKeepDatabase)at System.Data.Entity.Migrations.DbMigrator.Update(字符串 targetMigration) System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串 targetMigration) System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()