Reputation: 105
I get the following error message from Package Manager Console when trying to remove the refrence to a class:
The object 'FK_dbo.Resultats_dbo.Ovelses_OvelseId' is dependent on column 'OvelseId'. ALTER TABLE DROP COLUMN OvelseId failed because one or more objects access this column.
I get this problem when doing the following changes to my model
public class Resultater
{
public int Id { get; set; }
[Required]
public string AspNetUsersId { get; set; } //foreign key for users
//public Ovelser Ovelse { get; set; }
//[Required]
//public int OvelseId { get; set; }
[Required]
[Display(Name = "Dato")]
public DateTime Date { get; set; }
[Required]
public string Form { get; set; }
}
public class Ovelser
{
public int Id { get; set; }
[Required]
[Display(Name = "Øvelse")]
public OvelseType OvelseType { get; set; }
[Required]
public short OvelseTypeId { get; set; }
[Required]
public decimal Resultat { get; set; }
[Required]
[Display(Name = "Hvordan var utførelsen")]
public string Beskrivelse { get; set; }
}
Package Manager Console gives me the following code when I start a migration:
public override void Up()
{
DropForeignKey("dbo.Resultaters", "OvelseId", "dbo.Ovelsers");
DropIndex("dbo.Resultaters", new[] { "OvelseId" });
DropColumn("dbo.Resultaters", "OvelseId");
}
public override void Down()
{
AddColumn("dbo.Resultaters", "OvelseId", c => c.Int(nullable: false));
CreateIndex("dbo.Resultaters", "OvelseId");
AddForeignKey("dbo.Resultaters", "OvelseId", "dbo.Ovelsers", "Id", cascadeDelete: true);
}
I found a very similar problem "ALTER TABLE DROP COLUMN failed because one or more objects access this column" but I am unable to apply it.
Upvotes: 0
Views: 3641
Reputation: 9074
I was looking at DropForeignKey(). There is another version that allows you to specify the name of the foreign key. You might be able to add that to your migration. Microsoft document on DropForeignKey
DropForeignKey("dbo.Resultaters", "FK_dbo.Resultats_dbo.Ovelses_OvelseId");
Upvotes: 3
Reputation: 9074
Try this in SQL Server Management Studio and then do your migration.
use [Name Of Your Database];
alter table Resultater drop constraint [FK_dbo.Resultats_dbo.Ovelses_OvelseId];
Upvotes: 0