Error while executing Migrations

0

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.

    
asked by anonymous 02.10.2017 / 19:58

1 answer

0

The solution found was to return the migration to the previous point of the relationship, in my case it worked because the previous migration was to link the tables following the command

Update-Database -TargetMigration:201710021159389_Type_Delivery_Seeds_Raname_Restaurant_id -Verbose

Thanks everyone.

    
03.10.2017 / 00:06