Error While Running Migrations

Asked

Viewed 169 times

1

Friends, I have two entities Deliveryfee and City, where Deliveryfee has a FK from City, when I went to withdraw the relationship between the two and ran the Migration he returned me the following mistake.

object 'Fk_dbo.Deliveryfees_dbo.Cities_cities_id' depends on column 'Cityid'. ALTER TABLE DROP COLUMN Cityid fails because one or more objects access this column.

follows the Code of the migration generated file.

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 it has an extra relationship with Cityid that I’m removing but don’t have.

  • Both tables are empty?

  • only the City is populated

  • He is not able to drop the FK that makes the relationship with Cityid and when it comes time to drop the column, there is an integrity error that FK needs. I can’t tell you apart from the fact that there’s still a record between the two relating.

  • I erased even the City Records to see if it worked but nothing the same mistake.

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 follows the command

Update-Database -TargetMigration:201710021159389_Type_Delivery_Seeds_Raname_Restaurant_id -Verbose

Thanks to all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.