I have a created an MVC application using the code-first approach with an existing database.
I have noticed that one of my tables doesn't need a foreign key, so I have tried to delete it but I keep getting:
The object 'FK_BorrowedProperty_codeStatus' is dependent on column 'StatusId'. ALTER TABLE DROP COLUMN StatusId failed because one or more objects access this column.
Steps I have done
- Gone into
BorrowedProperty
class and deletedpublic int StatusId { get; set; }
&public virtual codeStatu codeStatu { get; set; }
- Went into
CodeStatu
class and deleted[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<BorrowedProperty> BorrowedProperties { get; set; }
- Gone into my DbContext and commented out this line
//modelBuilder.Entity<codeStatu>() // .HasMany(e => e.BorrowedProperties) // .WithRequired(e => e.codeStatu) // .HasForeignKey(e => e.StatusId) // .WillCascadeOnDelete(false);
- Add-Migration DeleteStatusFKFromBorrowedProperty
That results in:
public partial class DeleteStatusFKFromBorrowedProperty : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus");
DropIndex("dbo.BorrowedProperty", new[] { "StatusId" });
DropColumn("dbo.BorrowedProperty", "StatusId");
}
public override void Down()
{
AddColumn("dbo.BorrowedProperty", "StatusId", c => c.Int(nullable: false));
CreateIndex("dbo.BorrowedProperty", "StatusId");
AddForeignKey("dbo.BorrowedProperty", "StatusId", "dbo.codeStatus", "Id");
}
}
- update-database
Then I receive the error above. There are zero records in the BorrowedProperty database table.
What am I doing wrong?