title: how to move serialized xml data to a entity framework code first database?
body: <p>I have a xml file that i have created by serializing a object with the serializer.Serialize() method.</p>
  
  <p>The xml file has a structure simular to this:</p>
  
  <pre><code>&lt;book id="1"&gt;
   &lt;section id="1"&gt;
    &lt;chapter id="1"&gt;
     &lt;line id="1"&gt;
      text...
     &lt;/line&gt;
     &lt;line id="2"&gt;
      more text...
     &lt;/line&gt;
    &lt;/chapter&gt;
    &lt;chapter id="2"&gt;
     &lt;line id="1"&gt;
      text...
     &lt;/line&gt;
    &lt;/chapter&gt;
   &lt;/section&gt;
  &lt;/book&gt;
  </code></pre>
  
  <p>I would like to move the created xml to a MSSQL database and use the entity framework to handle database in a MVC site.</p>
  
  <p>I have tried to create the DB via EF in a MVC project.</p>
  
  <p>What i did was, that i loaded the data into a object. Then, using the dbcontext i tried to write data into new objects in the db context. 
  Everything went "ok", but when i called db.SaveChanges(), no data got saved in the database.</p>
  
  <p>Any help on how to solve my problem would be very nice.</p>
  
  <p>Code example:</p>
  
  <pre><code>// THE DBCONTEXT
  
  public class BookContext : DbContext
  {
      public DbSet&lt;DbBook&gt; Books { get; set; }
      public DbSet&lt;DbSection&gt; Sections { get; set; }
      public DbSet&lt;DbBook&gt; Books { get; set; }
      public DbSet&lt;DbChapter&gt; Chapters { get; set; }
      public DbSet&lt;DbVers&gt; Verses { get; set; }
      public DbSet&lt;DbCrossRefence&gt; CrossReferences { get; set; }
  
  }
  
   // TRYING TO WRITE DATA TO DB
  
   Book xmlBook = Book.Load("C://book.xml");
  
          using (var db = new BookContext())
          {
  
              DbBook book = new DbBook();
              book.Id = "My book";
  
              if (book.Sections == null)
              {
                  book.Sections = new List&lt;DbSection&gt;();
              }
  
              foreach (Section s in xmlBook.Sections)
              {
                  DbSection section = new DbSection();
                  section.Id = s.Id.ToString();
  
                  if (section.Books == null)
                  {
                      section.Books = new List&lt;DbBook&gt;();
                  }
  
                  foreach (Book b in s.Books)
                  {
                      DbBook book = new DbBook();
                      book.Id = b.Id;
  
                      if (book.Chapters == null)
                      {
                          book.Chapters = new List&lt;DbChapter&gt;();
                      }
  
                      foreach (Chapter c in b.Chapters)
                      {
                          DbChapter chapter = new DbChapter();
                          chapter.Id = c.Id;
  
                          if (chapter.Verses == null)
                          {
                              chapter.Verses = new List&lt;DbVers&gt;();
                          }
  
                          foreach (Vers v in c.Verses)
                          {
                              DbVers vers = new DbVers();
                              vers.Id = v.Id;
                              vers.Content = v.Content;
  
                              if (vers.CrossReferences == null)
                              {
                                  vers.CrossReferences = new List&lt;DbCrossRefence&gt;();
                              }
  
                              foreach (CrossReference cr in v.CrossReferences)
                              {
                                  DbCrossRefence crossreference = new DbCrossRefence();
                                  crossreference.RefId = cr.RefId;
                                  crossreference.Id = cr.Id;
                                  crossreference.Book = cr.Book;
                                  crossreference.Chapter = cr.Chapter;
  
                                  try
                                  {
                                      crossreference.Vers = cr.Vers;
                                  }
                                  catch (Exception ex)
                                  {
  
                                  }
  
  
                                  vers.CrossReferences.Add(crossreference);
                              }
  
                              chapter.Verses.Add(vers);
                          }
  
                          book.Chapters.Add(chapter);
                      }
  
                      section.Books.Add(book);
                  }
  
                  book.Sections.Add(section);
              }
  
              db.SaveChanges();
          }
  </code></pre>