Friends, I have two DeliveryFee and City entities, where DeliveryFee has a City FK, when I went to remove the relationship between the two and I managed the Migration it returned the following error.
'FK_dbo.DeliveryFees_dbo.Cities_Cities_Id' object depends on column 'CityId'. ALTER TABLE DROP COLUMN CityId failed because one or more objects access this column.
follows the generated migration file code.
public partial class Remove_City_DeliveryFee : DbMigration
{
public override void Up()
{
DropForeignKey("dbo.DeliveryFees", "CityId", "dbo.Cities");
DropIndex("dbo.DeliveryFees", new[] { "CityId" });
DropColumn("dbo.DeliveryFees", "CityId");
}
public override void Down()
{
AddColumn("dbo.DeliveryFees", "CityId", c => c.Long(nullable: false));
CreateIndex("dbo.DeliveryFees", "CityId");
AddForeignKey("dbo.DeliveryFees", "CityId", "dbo.Cities", "Id");
}
}
and my classes.
public class DeliveryFee : BaseModel
{
public float Distance { get; set; }
public decimal Price { get; set; }
//[Required]
//public long CityId { get; set; }
[Required]
public long BranchId { get; set; }
//public virtual City Cities { get; set; }
public virtual Branch Branch { get; set; }
}
public class City : BaseModel
{
public City()
{
ZipCodes = new HashSet<Neighborhood>();
//DeliveryFees = new HashSet<DeliveryFee>();
}
[StringLength(80)]
public string Description { get; set; }
[Required]
[ForeignKey("State")]
public long StateId { get; set; }
public virtual State State { get; set; }
public virtual ICollection<Neighborhood> ZipCodes { get; set; }
//public virtual ICollection<DeliveryFee> DeliveryFees { get; set; }
}
Entity complains that you have an extra relationship with CityId that I'm removing, but I do not have one.