0

I'm getting the error

The member with identity 'UsersModel_Group' does not exist in the metadata collection. Parameter name: identity.

when calling the db.UsersModel.Add(SomeModel) method. I checked the controllers and models if there's any problem with namespaces but there's nothing weird that I found. I did change some classes name but there are no errors upon build. Any suggestions?

here are the classes that might be relevant:

public class UsersModel{
   public int ID {get; set;}
   ...
   [ForeignKey("GroupID")]
   [Display(Name = "Group")]
   public virtual GroupModel Group { get; set; }

   [Required]
   public virtual int GroupID { get; set; }

   public virtual List<GroupModel> GroupList { get; set; }
}

   public class GroupModel {
     public int ID { get; set;}
     ...
     [Required]
     public int ID { get; set; }

     [Required]
     [StringLength(20,ErrorMessage = "The {0} must not exceed {2} characters")]
     public string Name { get; set; }
 }

1 Answer 1

1

If you've changed your model after the database was created you will need to recreate the database. EF Code First stores details of your model inside a metadata table. If this metadata does not match that of your model then you will get an error.

If you don't want to drop your database you can delete the metadata table and then manually make your database changes so that it matches your model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.