简体   繁体   中英

how to move serialized xml data to a entity framework code first database?

I have a xml file that i have created by serializing a object with the serializer.Serialize() method.

The xml file has a structure simular to this:

<book id="1">
 <section id="1">
  <chapter id="1">
   <line id="1">
    text...
   </line>
   <line id="2">
    more text...
   </line>
  </chapter>
  <chapter id="2">
   <line id="1">
    text...
   </line>
  </chapter>
 </section>
</book>

I would like to move the created xml to a MSSQL database and use the entity framework to handle database in a MVC site.

I have tried to create the DB via EF in a MVC project.

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.

Any help on how to solve my problem would be very nice.

Code example:

// THE DBCONTEXT

public class BookContext : DbContext
{
    public DbSet<DbBook> Books { get; set; }
    public DbSet<DbSection> Sections { get; set; }
    public DbSet<DbBook> Books { get; set; }
    public DbSet<DbChapter> Chapters { get; set; }
    public DbSet<DbVers> Verses { get; set; }
    public DbSet<DbCrossRefence> 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<DbSection>();
            }

            foreach (Section s in xmlBook.Sections)
            {
                DbSection section = new DbSection();
                section.Id = s.Id.ToString();

                if (section.Books == null)
                {
                    section.Books = new List<DbBook>();
                }

                foreach (Book b in s.Books)
                {
                    DbBook book = new DbBook();
                    book.Id = b.Id;

                    if (book.Chapters == null)
                    {
                        book.Chapters = new List<DbChapter>();
                    }

                    foreach (Chapter c in b.Chapters)
                    {
                        DbChapter chapter = new DbChapter();
                        chapter.Id = c.Id;

                        if (chapter.Verses == null)
                        {
                            chapter.Verses = new List<DbVers>();
                        }

                        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<DbCrossRefence>();
                            }

                            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();
        }

i manage to get it working now :-)

What i did for starters, was to save the data to the database a little at a time. First the book. Then the chapters. and so on, and so on.

And i move the:

db.SaveChanges()

into the nested foreach loops, so that data got saved to the database while looping, insteed of waiting until the end. I suspect that it was just to much data to handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM