entity framework - Migrations fails to downgrade -
using add-migration, next code-first migration:
public partial class mymigration : dbmigration { public override void up() { dropforeignkey("dbo.attachment", "employeepresentationid", "dbo.l_employeepresentation"); dropforeignkey("dbo.attachment", "topicid", "dbo.topic"); dropforeignkey("dbo.attachment", "uploaderid", "dbo.user"); } public override void down() { addforeignkey("dbo.attachment", "uploaderid", "dbo.user", "id", cascadedelete: true); addforeignkey("dbo.attachment", "topicid", "dbo.topic", "id"); addforeignkey("dbo.attachment", "employeepresentationid", "dbo.l_employeepresentation", "id"); } }
the code shortened, not modified. while up-migration works well, down-migration fails:
pm> update-database -verbose using startup project 'my_project'. using nuget project 'my_project'. specify '-verbose' flag view sql statements beingness applied target database. target database is: 'my-database' (datasource: (localdb)\v11.0, provider: system.data.sqlclient, origin: configuration). applying explicit migrations: [201410131339480_mymigration]. applying explicit migration: 201410131339480_mymigration. if object_id(n'[dbo].[fk_dbo.attachedfile_dbo.l_emppr_empprid]', n'f') not null alter table [dbo].[attachedfile] drop constraint [fk_dbo.attachedfile_dbo.l_emppr_empprid] if object_id(n'[dbo].[fk_dbo.attachedfile_dbo.topic_topicid]', n'f') not null alter table [dbo].[attachedfile] drop constraint [fk_dbo.attachedfile_dbo.topic_topicid] if object_id(n'[dbo].[fk_dbo.attachedfile_dbo.user_uploaderid]', n'f') not null alter table [dbo].[attachedfile] drop constraint [fk_dbo.attachedfile_dbo.user_uploaderid] insert [dbo].[__migrationhistory]([migrationid], [contextkey], [model], [productversion]) values (n'201410131339480_mymigration', n'my_project.datalayer.datacontext', /// loooooooong hex-stuff running seed method. pm> update-database -target tags_added -verbose using startup project 'my_project'. using nuget project 'my_project'. specify '-verbose' flag view sql statements beingness applied target database. target database is: 'my-database' (datasource: (localdb)\v11.0, provider: system.data.sqlclient, origin: configuration). reverting migrations: [201410131339480_mymigration]. reverting explicit migration: 201410131339480_mymigration. alter table [dbo].[attachedfile] add together constraint [fk_dbo.attachedfile_dbo.user_uploaderid] foreign key ([uploaderid]) references [dbo].[user] ([id]) on delete cascade system.data.sqlclient.sqlexception (0x80131904): introducing foreign key constraint 'fk_dbo.attachedfile_dbo.user_uploaderid' on table 'attachedfile' may cause cycles or multiple cascade paths. specify on delete no action or on update no action, or modify other foreign key constraints. not create constraint. see previous errors. .... introducing foreign key constraint 'fk_dbo.attachedfile_dbo.user_uploaderid' on table 'attachedfile' may cause cycles or multiple cascade paths. specify on delete no action or on update no action, or modify other foreign key constraints. not create constraint. see previous errors. pm>
the error baffles me, because database has foreign key @ moment. if impossible, shouldn't have current database, right?
here table structure:
/****** object: table [dbo].[attachedfile] script date: 10/14/2014 14:05:55 ******/ set ansi_nulls on go set quoted_identifier on go create table [dbo].[attachedfile]( [id] [int] identity(1,1) not null, [topicid] [int] null, [deleted] [datetime] null, [displayname] [nvarchar](max) not null, [safename] [nvarchar](max) not null, [extension] [nvarchar](max) not null, [created] [datetime] not null, [filesize] [int] not null, [uploaderid] [int] not null, [employeepresentationid] [int] null, constraint [pk_dbo.attachedfile] primary key clustered ( [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go alter table [dbo].[attachedfile] check add together constraint [fk_dbo.attachedfile_dbo.l_employeepresentation_employeepresentation_id] foreign key([employeepresentationid]) references [dbo].[l_employeepresentation] ([id]) go alter table [dbo].[attachedfile] check constraint [fk_dbo.attachedfile_dbo.l_employeepresentation_employeepresentation_id] go alter table [dbo].[attachedfile] check add together constraint [fk_dbo.attachedfile_dbo.topic_topicid] foreign key([topicid]) references [dbo].[topic] ([id]) go alter table [dbo].[attachedfile] check constraint [fk_dbo.attachedfile_dbo.topic_topicid] go alter table [dbo].[attachedfile] check add together constraint [fk_dbo.attachedfile_dbo.user_uploader_id] foreign key([uploaderid]) references [dbo].[user] ([id]) on delete cascade go alter table [dbo].[attachedfile] check constraint [fk_dbo.attachedfile_dbo.user_uploader_id] go /****** object: table [dbo].[user] script date: 10/14/2014 14:06:00 ******/ set ansi_nulls on go set quoted_identifier on go create table [dbo].[user]( [id] [int] identity(1,1) not null, [guid] [uniqueidentifier] not null, [shortname] [nvarchar](max) not null, [longname] [nvarchar](max) null, [emailaddress] [nvarchar](max) null, [isactive] [bit] not null, [activesession_id] [int] null, [settings_colorscheme] [int] not null, [sessionreport_id] [int] null, [settings_reportoccasions] [int] not null, constraint [pk_dbo.user] primary key clustered ( [id] asc )with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary] ) on [primary] go alter table [dbo].[user] check add together constraint [fk_dbo.user_dbo.activesession_activesession_id] foreign key([activesession_id]) references [dbo].[activesession] ([id]) go alter table [dbo].[user] check constraint [fk_dbo.user_dbo.activesession_activesession_id] go alter table [dbo].[user] check add together constraint [fk_dbo.user_dbo.sessionreport_sessionreport_id] foreign key([sessionreport_id]) references [dbo].[sessionreport] ([id]) go alter table [dbo].[user] check constraint [fk_dbo.user_dbo.sessionreport_sessionreport_id] go alter table [dbo].[user] add together default ((0)) [settings_colorscheme] go alter table [dbo].[user] add together default ((0)) [settings_reportoccasions] go
if foreign key on dependent entity not nullable, code first sets cascade delete on relationship.
reference: ef cascade on delete
so downwards migration adding cascade on delete didn't exist before migration. utilize fluentapi method .willcascadeondelete(false)
specify that foreign key should not cascade on delete.
entity-framework code-first-migrations
No comments:
Post a Comment