Example #1
0
        private static void AddDatatoModel()
        {
            using (var ctx = new EFRecipesEntities())
            {
                var poem = new Poem {
                    Title = "LOT2"
                };
                var poet = new Poet {
                    FirstName = "John", LastName = "Milton"
                };
                var meter = new Meter {
                    MeterName = "meterName"
                };
                poem.Meter = meter;
                poem.Poet  = poet;
                ctx.Poems.Add(poem);

                poet = new Poet {
                    FirstName = "Lewis", LastName = "Carroll"
                };
                poem = new Poem {
                    Title = "TheHunt"
                };
                meter = new Meter {
                    MeterName = "Anape"
                };
                poem.Meter = meter;
                poem.Poet  = poet;
                ctx.Poems.Add(poem);
                // ctx.SaveChanges();
                try
                {
                    ctx.SaveChanges();
                }

                catch (Exception gex)
                {
                    if (gex is DbEntityValidationException)
                    {
                        var ex = gex as DbEntityValidationException;
                        foreach (var entityValidationErrors in ex.EntityValidationErrors)
                        {
                            foreach (var validationError in entityValidationErrors.ValidationErrors)
                            {
                                Console.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                            }
                        }
                    }
                    else
                    {
                        var excep = gex as DbUpdateException;

                        var e = gex.InnerException;
                        Console.WriteLine(e.InnerException.Message);
                        Console.WriteLine(excep.Source.ToString());
                    }
                }
            }
        }
Example #2
0
 private static void ViewDataRec2()
 {
     using (var ctx = new EFRecipesEntities())
     {
         foreach (var item in ctx.Poems)
         {
             Console.WriteLine(item.Title + "   " + item.Poet.FirstName);
         }
     }
 }