Ehsan Akbar
Ehsan Akbar

Reputation: 7281

Create view in MVC causes an error

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,

Upvotes: 0

Views: 97

Answers (1)

ta.speot.is
ta.speot.is

Reputation: 27214

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.

Upvotes: 1

Related Questions