Computer Magic Logo
Insert data

Sunday, August 23, 2015

Published by Aristotelis Pitaridis

Now that everything is ready let's insert some data to our database. Below there is the required code which can be added in a view in order to add two authors. The first author is "Jules Verne" and the second is "Douglas Adams".

We also add some books. We add three books to "Jules Verne" and two books to "Doublas Adams". We left the description for the books of "Doublas Adams" equal to null so that we will see that we can have a nullable field if we declare it in our model.

@using ComputerMagic.PetaPoco.Models
@using ComputerMagic.PetaPoco.Repository
@{ 
    Layout = null;

    var JulesVerne = new Author()
    {
        Name = "Jules",
        Surname = "Verne",
        DateOfBirth = new DateTime(1828, 2, 8)
    };
    Authors.Save(JulesVerne);

    var JulesVerneBook1 = new Book()
    {
        Title = "Journey to the Center of the Earth",
        Description = "The story involves German professor Otto Lidenbrock who believes there are volcanic tubes going toward the centre of the Earth. He, his nephew Axel, and their guide Hans descend into the Icelandic volcano Sn?fellsjokull, encountering many adventures, including prehistoric animals and natural hazards, before eventually coming to the surface again in southern Italy, at the Stromboli volcano.",
        Year = 1864,
        AuthorID = JulesVerne.AuthorID
    };
    Books.Save(JulesVerneBook1);

    var JulesVerneBook2 = new Book()
    {
        Title = "Twenty Thousand Leagues Under the Sea",
        Description = "It tells the story of Captain Nemo and his submarine Nautilus, as seen from the perspective of Professor Pierre Aronnax after he, his servant Conseil, and Canadian whaler Ned Land wash up on their ship. On the Nautilus, the three embark on an undersea journey which takes them around the world.",
        Year = 1870,
        AuthorID = JulesVerne.AuthorID
    };
    Books.Save(JulesVerneBook2);

    var JulesVerneBook3 = new Book()
    {
        Title = "Around the World in Eighty Days",
        Description = "In the story, Phileas Fogg of London and his newly employed French valet Passepartout attempt to circumnavigate the world in 80 days on a £20,000 wager (roughly £1.6 million today) set by his friends at the Reform Club. It is one of Verne's most acclaimed works.",
        Year = 1873,
        AuthorID = JulesVerne.AuthorID
    };
    Books.Save(JulesVerneBook3);

    var DouglasAdams = new Author()
    {
        Name = "Douglas",
        Surname = "Adams",
        DateOfBirth = new DateTime(1952, 3, 11)
    };
    Authors.Save(DouglasAdams);

    var DouglasAdamsBook1 = new Book()
    {
        Title = "The Hitchhiker's Guide to the Galaxy",
        Year = 1978,
        AuthorID = DouglasAdams.AuthorID
    };
    Books.Save(DouglasAdamsBook1);

    var DouglasAdamsBook2 = new Book()
    {
        Title = "Dirk Gently's Holistic Detective Agency",
        Year = 1987,
        AuthorID = DouglasAdams.AuthorID
    };
    Books.Save(DouglasAdamsBook2);
}

After executing this code the data will be stored in our tables and the contents of the Authors table will be the following.

The contents of the Books table will be the following.