﻿BEGIN TRANSACTION;
DROP TABLE [IscrizioneEventoPerMail];

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260122105724_Rimozione_IscrizioneEventoPerMail', N'9.0.11');

ALTER TABLE [Utente] ADD [ProvinciaIstatId] uniqueidentifier NULL;

ALTER TABLE [IscrizioneEvento] ADD [AgenteId] uniqueidentifier NULL;

ALTER TABLE [IscrizioneEvento] ADD [CapoareaId] uniqueidentifier NULL;

CREATE INDEX [IX_Utente_ProvinciaIstatId] ON [Utente] ([ProvinciaIstatId]);

CREATE INDEX [IX_IscrizioneEvento_AgenteId] ON [IscrizioneEvento] ([AgenteId]);

CREATE INDEX [IX_IscrizioneEvento_CapoareaId] ON [IscrizioneEvento] ([CapoareaId]);

ALTER TABLE [IscrizioneEvento] ADD CONSTRAINT [FK_IscrizioneEvento_Utente_AgenteId] FOREIGN KEY ([AgenteId]) REFERENCES [Utente] ([Id]);

ALTER TABLE [IscrizioneEvento] ADD CONSTRAINT [FK_IscrizioneEvento_Utente_CapoareaId] FOREIGN KEY ([CapoareaId]) REFERENCES [Utente] ([Id]);

ALTER TABLE [Utente] ADD CONSTRAINT [FK_Utente_Province_ProvinciaIstatId] FOREIGN KEY ([ProvinciaIstatId]) REFERENCES [Province] ([Id]);

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260204162205_CapiareaAgentiInIscrizioneEvento', N'9.0.11');

ALTER TABLE [Province] ADD [CapoareaDaVerificare] bit NOT NULL DEFAULT CAST(0 AS bit);

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260205095709_CapoareaDaVerificareInProvincia', N'9.0.11');

ALTER TABLE [Utente] DROP CONSTRAINT [FK_Utente_Province_ProvinciaIstatId];

DROP INDEX [IX_Utente_ProvinciaIstatId] ON [Utente];

DECLARE @var sysname;
SELECT @var = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Utente]') AND [c].[name] = N'ProvinciaIstatId');
IF @var IS NOT NULL EXEC(N'ALTER TABLE [Utente] DROP CONSTRAINT [' + @var + '];');
ALTER TABLE [Utente] DROP COLUMN [ProvinciaIstatId];

ALTER TABLE [Province] ADD [CapoareaId] uniqueidentifier NULL;

CREATE INDEX [IX_Province_CapoareaId] ON [Province] ([CapoareaId]);

ALTER TABLE [Province] ADD CONSTRAINT [FK_Province_Utente_CapoareaId] FOREIGN KEY ([CapoareaId]) REFERENCES [Utente] ([Id]);

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260206070903_CapoareaSuProvIstat', N'9.0.11');

COMMIT;
GO

