سلام و تشکر از پشتیبانی خوب و دورهی خوبتون. زمانی که میخواهم دیتابیس را آپدیت کنم، جدول Choice2Surveys این ارور را میدهد:
PM> Update-Database
Build started...
Build succeeded.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 6.0.3 initialized 'SurveyfaContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.3' with options: None
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (111ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (238ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (50ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (33ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Applying migration '20220415071038_Init'.
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Microsoft.EntityFrameworkCore.Migrations[20402]
Applying migration '20220415071038_Init'.
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (146ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Students] (
[Id] int NOT NULL IDENTITY,
[Counter] nvarchar(max) NOT NULL,
[NationalCode] nvarchar(max) NOT NULL,
CONSTRAINT [PK_Students] PRIMARY KEY ([Id])
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (15ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Surveys] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NOT NULL,
CONSTRAINT [PK_Surveys] PRIMARY KEY ([Id])
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Choices] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NOT NULL,
[SurveyId] int NOT NULL,
[Votes] int NOT NULL,
CONSTRAINT [PK_Choices] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Choices_Surveys_SurveyId] FOREIGN KEY ([SurveyId]) REFERENCES [Surveys] ([Id]) ON DELETE CASCADE
);
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (19ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Choice2Surveys] (
[Id] int NOT NULL,
CONSTRAINT [PK_Choice2Surveys] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Choice2Surveys_Choices_Id] FOREIGN KEY ([Id]) REFERENCES [Choices] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_Choice2Surveys_Surveys_Id] FOREIGN KEY ([Id]) REFERENCES [Surveys] ([Id]) ON DELETE CASCADE
);
Failed executing DbCommand (19ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Choice2Surveys] (
[Id] int NOT NULL,
CONSTRAINT [PK_Choice2Surveys] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Choice2Surveys_Choices_Id] FOREIGN KEY ([Id]) REFERENCES [Choices] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_Choice2Surveys_Surveys_Id] FOREIGN KEY ([Id]) REFERENCES [Surveys] ([Id]) ON DELETE CASCADE
);
Microsoft.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Choice2Surveys_Surveys_Id' on table 'Choice2Surveys' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:7572e4da-c537-4d97-8299-dfd7c0636c87
Error Number:1785,State:0,Class:16
Introducing FOREIGN KEY constraint 'FK_Choice2Surveys_Surveys_Id' on table 'Choice2Surveys' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
این جدول لینک بین ۲ جدول Choice و Survey هست.
با تشکر از پاسخگوییتون 💐
سلام و تشکر.
با این لینک مشکل ارور کامند حل شد.
اما موقع اسکریپت زدن این ارور میآد:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Choice2Surveys_Choices_ChoiceId". The conflict occurred in database "Surveyfa", table "dbo.Choices", column 'Id'.
The statement has been terminated.
فایل اسکریپت هم این هست:
SET IDENTITY_INSERT [dbo].[Choice2Surveys] ON
INSERT INTO [dbo].[Choice2Surveys] ([Id], [ChoiceId], [SurveyId]) VALUES (1, 1, 1)
SET IDENTITY_INSERT [dbo].[Choice2Surveys] OFF
با تشکر فراوان.
سلام خواهش میکنم دوست من
شما دارین داخل جدولتون دیتایی وارد میکنید که ایدی foreign key که دارین وارد میکنید معتبر نیست و احتمالا اون ایدی داخل جدول خودش وجود نداره
توجه کنید شما زمانی که بین دوتا جدول رابطه میزنید و کلید خارجی قرار میدید اون کلید خارجی نمیتونه ایدی باشه که داخل جدول خودش وجود نداشته باشه