1

I am trying to insert classifications data into a Classifications table in my database with Entity framework. The issue is that it gives me this error:

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

This is my code:

 public class Classifications
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Required]
        public virtual int id { get; set; }

        [Required]
        [MaxLength(250)]
        public virtual string Classification { get; set; }

        [ForeignKey("Movie")]
        [Required]
        public virtual List<Movie> Movies { get; set; }
        [Required]
        public virtual List<Series> Series { get; set; }
        [Required]
        public virtual List<Preference> Preferences { get; set; }
}

and here is my method for adding classifications:

 public static void InsertClassification(string name)
        {
            try
            {
                var classification = new Classifications() { Classification = name };
                using (var db = new DatabaseContext())
                {
                    db.Classifications.Add(classification);
                    db.SaveChanges();
                    Console.WriteLine("Added Classification");
                }
            }catch(Exception ex)
            {
                Console.WriteLine("Cound not add classification");
            }
        }

The error is being given on the db.saveChanges(). With debug it checks this method but instead to print the added data message it goes to the Exception.

I am running it on my Main methoh.

 class Program
    {
        static void Main(string[] args)
        {
           string name = "ClassificationTest";
           Classifications.InsertClassification(name);


        }
    }

Can somebody help me solve this issue ?

4
  • 1
    It may be caused by the [Required] tag on top of the Movies, Series and Preferences. Commented Jan 17, 2021 at 18:05
  • 1
    And what are the EntityValidationErrors? Check via ex via the Locals window Commented Jan 17, 2021 at 18:06
  • In the locals window it just says : System.Exception {System.Data.Entity.Validation.DbEntityValidationException} Commented Jan 17, 2021 at 18:09
  • 1
    Yes it was because of the [Required] fields. Commented Jan 17, 2021 at 18:12

1 Answer 1

2

You have to remove Required for this properties or create these lists before SaveChanges()

 [ForeignKey("Movie")]
        [Required]
        public virtual List<Movie> Movies { get; set; }
        [Required]
        public virtual List<Series> Series { get; set; }
        [Required]
        public virtual List<Preference> Preferences { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Would you care to explain why?
It is written in my answer already. Attribute [Requiered] demands that property should not be null. For example Movies=new List<Movies>{ ...}

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.