0

I have a student class like this :

namespace DomainClasses
{
    using System;
    using System.Collections.Generic;

    public partial class Student
    {
        public Student()
        {
            this.Scores = new HashSet<Score>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
        public string IntNo { get; set; }
        public string FatherName { get; set; }
        public string BirthLocation { get; set; }
        public string Birthday { get; set; }
        public string ImageUrl { get; set; }
        public string Major { get; set; }
        public string Degree { get; set; }
        public string IdentNo { get; set; }
        public string Address { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public System.DateTime RegisterDate { get; set; }
        public string StudentId { get; set; }
        public string State { get; set; }

        public virtual ICollection<Score> Scores { get; set; }
    }
}

This class has a repository class to handle requests :

public  class StudentRepository
{
        readonly EducationDBEntities _dbcontext = new EducationDBEntities();

        public void AddNewStudent(Student student)
        {
            _dbcontext.Students.Add(student);
        }

        public void RemoveStudent(Student student)
        {
            _dbcontext.Students.Remove(student);
        }

        public List<Student> GetStudentlist()
        {
            return _dbcontext.Students.ToList();
        }

        public Student FindStudentById(int id)
        {
            return _dbcontext.Students.Find(id);
        }

        public void Update(Student student)
        {
            _dbcontext.Entry(student).State = EntityState.Modified;
        }

        public void Save()
        {
            _dbcontext.SaveChanges();
        }
}

When I try to create a student model using create view in MVC I got this error :

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

So this exception comes from the Save method of the repository class. I mean when I want to save the context I got this error.

My student controller to create the model:

[HttpPost]
public ActionResult Create(Student student)
{
     obj.AddNewStudent(student);
     obj.Save();
     return RedirectToAction("Index", "Student");
}

I create this model using EF6,

6
  • So what does 'EntityValidationErrors' say? Commented Mar 23, 2014 at 9:25
  • Did you see the EntityValidationErrors for more details? Commented Mar 23, 2014 at 9:25
  • how does your DB Scheme for student looks like? Commented Mar 23, 2014 at 9:26
  • @Szymon:how can i do that ?No Commented Mar 23, 2014 at 9:27
  • I can't find EntityValidationErrors :(( Commented Mar 23, 2014 at 9:30

1 Answer 1

1

See 'EntityValidationErrors' property for more details.

Try seeing the EntityValidationErrors property for more details. Replace _dbcontext.SaveChanges(); with

try
{
    _dbcontext.SaveChanges();
}
catch (DbEntityValidationException validationException)
{
     // Just use the debugger to inspect validationException
     // here, or output EntityValidationErrors in some way.
}    // <== You can set a breakpoint here.
Sign up to request clarification or add additional context in comments.

6 Comments

It says .the student id field is required ?why ?
It's auto increament makes me think you're using MySQL and not MSSQL. It shouldn't be necessary but you might try annotating Id with [DatabaseGenerated(DatabaseGenerationOption.Identity)]
As you can see in my student class i have this field.but the mvc doesn't include it in create view ?
I was wrong sorry .it isn't auto increament ,the mvc doesn't include it in view
Well it doesn't have an ID until after it's created.
|

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.