Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
I'm using code first and fluent api this is my models

public Admin()
        {
            this.course = new HashSet<Courses>();
        }
        [Key]
        public int ID { get; set; }
   public string LoginName { get; set; }
   public virtual ICollection<Courses> course { get; set; }
}

 public Courses()
            {
                this.admin = new HashSet<Admin>();
            }
            [Key]

            public int ID { get; set; }
      public string Name { get; set; }
     public virtual ICollection<Admin> admin { get; set; }
    }
and this is fluent api

modelBuilder.Entity<Admin>()
                .HasMany(e => e.course)
                .WithMany(e => e.admin);
and this is my controller which I'm getting the courseid from cshml

public ActionResult Admins( Admin rec, IList CourseId) {

      foreach (var item in CourseId)
        {
            var cr = new Courses();
            cr.ID = item;
            rec.course.Add(cr);
 }
  db.Admins.Add(rec);
  db.SaveChanges();
it gives me error when I'm trying to save db.SaveChanges()

could any one tell me what's the wrong here ?


What I have tried:

I don't know what should I try
Posted
Updated 27-Dec-16 18:40pm
Comments
ZurdoDev 27-Dec-16 8:15am    
Is there a reason you did not post the error?
Sarah Mohammed77 27-Dec-16 8:17am    
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
ZurdoDev 27-Dec-16 14:54pm    
1. Reply to the comment so that the user is notified. I just happened to come back here and see your comment.
2. Google the error. So simple. Tons of solutions online. See http://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert

1 solution

This is because, you are trying to add the value in the record in incorrect order
think, you have different order in DB and that is not matching with the parameter type which you are passing through this code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900