All containing foreign keys must be removed or redefined before the property can be removed - EF Core

Viewed 2929

I am getting the above error when trying to add migration after add a foreign key using Entity Framework core. I am adding FK in

public class ApplicantDetail
    {
        [Key]
        public int Id { get; set; }
        [ForeignKey("GrantProgramFK")]
        public GrantProgram GrantProgramId { get; set; }
 --------
   }

This FK ties to class

public class GrantProgram
    {
        [Key]
        public int Id { get; set; }
   -----
    }

Any help is appreciated. I tried to remove the entity and run migration again, but failed.

Full error:

The property 'GrantProgramId' cannot be removed from entity type 'EFDataAccessLibrary.Models.ApplicantDetail' because it is being used in the foreign key {'GrantProgramId'} on 'EFDataAccessLibrary.Models.ApplicantDetail'. All containing foreign keys must be removed or redefined before the property can be removed

I found a page, click here, but what does that mean? how to resolve it.

6 Answers

I had the same problem. I solved it by removing the "id" in the foreign key:

public class ApplicantDetail
    {
        [Key]
        public int Id { get; set; }
        [ForeignKey("GrantProgramFK")]
        public GrantProgram GrantProgram { get; set; }
 --------
   }

I still don't know why. If anybody can explain, or give a solution to leave "id" in it, don't hesitate to tell us.

Since entityframework auomatically uses any identifoer ending with 'ID or Id or id', ensure no other property name ends with 'ID or Id or id'

You have to fix your classes before migration:

public class ApplicantDetail
    {
        [Key]
        public int Id { get; set; }
         public int? GrantProgramId { get; set; }
        [ForeignKey("GrantProgramId")]
        [InverseProperty("ApplicantDetails")]
        public GrantProgram GrantProgram{ get; set; }
 --------
   }

public class GrantProgram
    {
        [Key]
        public int Id { get; set; }
       [InverseProperty(nameof(ApplicantDetail.GrantProram))]
        public virtual ICollection<ApplicantDetail> ApplicantDetails{ get; set; }
   -----
    }

I'd recommend to delete previous migrations file in the solution, this works for me

I was getting the same error, in my case all I did was undo the changes in AppDbContextModelSnapshot file (you can also delete the file) which is created in the migrations folder.

Turns out I had made an error before performing the migration, after which a model snapshot of my AppDbContext was created. After fixing the error, I tried the migration again, but the AppDbContextModelSnapshot was already modified, EF was not allowing to override with the same key of the same model (in this case GrantProgramFK from GrantProgram model) . So just undo the changes in your AppDbContextModelSnapshot file and perform the migration again.

I was able to solve the issue by going into ModelSnapShot.cs in the Migrations folder and making sure to delete all references to the property causing issues. You'll be looking for any references to 'GrantProgramIdId'.

Make sure to delete any lines where the snapshot creates foreign keys.

Change any instance where the property name includes the incorrect property name above.

Make sure you check the entire file. There should be two separate places where issues exist.

Also, delete any migrations where this property was included, then create a new migration and should be good to go.

Related