none
Cant insert Related entities using CTF RRS feed

  • Question

  • Hi I am newbite to CTP and I run into many problems.Here is one of them :

    I have decleared following entites :

    public partial class Catalog
      {
        public int ID { get; set; }
    
     public virtual ICollection<Pic> Pictures { get; set; }  
    }
    public partial class Pic
      {
        public int ID { get; set; }
    
     public int? CatalogID { get; set; }
    
        public virtual Catalog Catalog { get; set; }
    }
    

    And I Use following code to insert new catalog with new picture for it:

    var picItem=new Pic(){initialize properties}
    
    var catalog=new Catalog(){initialize properties}
    
     // Without {initializing Pictures the application throws an exception, Why ?
     catalog.Pictures=new List<Pic>();
    
     catalog.Pictures.Add(picItem);
    
    
    unitofwork.CatalogRepository.Insert(catalog);
    
    unitofwork.Save();
    

    I use unitofwork pattern to deal with context. When the application reaches 'unitofwork.Save();' , it throws :

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

    And when I see the exception detials, there is nothing there.

    Thursday, August 25, 2011 2:57 PM

Answers

  • Can you provide a minimal (i.e. without the unit of work pattern overhead) but complete repro? I tried and came up with the following:

    namespace Test
    {
      public partial class Catalog
      {
        public int ID { get; set; }
    
        public virtual ICollection<Pic> Pictures { get; set; }
      }
      public partial class Pic
      {
        public int ID { get; set; }
    
        public int? CatalogID { get; set; }
    
        public virtual Catalog Catalog { get; set; }
      }
    
      public class Context : DbContext
       {
        public DbSet<Catalog> Catalog { get; set; }
       }
     
      class Program
       {
         static void Main(string[] args)
         {
           Database.SetInitializer(new DropCreateDatabaseAlways<Context>());
    //       using (var trx = new TransactionScope())
           {
             using (var context = new Context())
             {
               var pic = new Pic { ID = 1 };
               var catalog = new Catalog { ID = 1 };
               catalog.Pictures = new List<Pic>();
               catalog.Pictures.Add(pic);
               context.Catalog.Add(catalog);
               context.SaveChanges();
             }
           }
         }
       }
    }

     

    and it did not repro so there must be something I am missing. Also take a look at the DbValidationException's EntityValidationErrors property as recommended in the exception message. It should tell you what property could not be validated and why.

    Pawel

    Friday, August 26, 2011 10:37 PM

All replies

  • Can you provide a minimal (i.e. without the unit of work pattern overhead) but complete repro? I tried and came up with the following:

    namespace Test
    {
      public partial class Catalog
      {
        public int ID { get; set; }
    
        public virtual ICollection<Pic> Pictures { get; set; }
      }
      public partial class Pic
      {
        public int ID { get; set; }
    
        public int? CatalogID { get; set; }
    
        public virtual Catalog Catalog { get; set; }
      }
    
      public class Context : DbContext
       {
        public DbSet<Catalog> Catalog { get; set; }
       }
     
      class Program
       {
         static void Main(string[] args)
         {
           Database.SetInitializer(new DropCreateDatabaseAlways<Context>());
    //       using (var trx = new TransactionScope())
           {
             using (var context = new Context())
             {
               var pic = new Pic { ID = 1 };
               var catalog = new Catalog { ID = 1 };
               catalog.Pictures = new List<Pic>();
               catalog.Pictures.Add(pic);
               context.Catalog.Add(catalog);
               context.SaveChanges();
             }
           }
         }
       }
    }

     

    and it did not repro so there must be something I am missing. Also take a look at the DbValidationException's EntityValidationErrors property as recommended in the exception message. It should tell you what property could not be validated and why.

    Pawel

    Friday, August 26, 2011 10:37 PM
  • After checking  DbValidationException, I found the problem was with on of required properties which has not been set. Thanks
    Saturday, August 27, 2011 9:12 AM