Demo of ADO.NET POCO with SQLite database

This demonstration shows how to prepare for and how to implement model-first ADO.NET POCO objects with a SQLite backing store.

C# (46.5 KB)
 
 
 
 
 
5 Star
(1)
3,378 times
Add to favorites
9/12/2011
E-mail Twitter del.icio.us Digg Facebook

using System.Collections.Generic;
using System.Linq;
using System.Text;
using POCOWithSQLiteDemo.Model;
using System;

namespace POCOWithSQLiteDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            Blog myBlog = new Blog
            {
                Name = "Menno's Blog"
            };

            Post p = new Post
            {
                Content = "My first post",
                Author = "Menno",
                Date = DateTime.Now
            };

            Review r = new Review
            {
                Content = "Excellent review",
                Rating = "95%",
                Author = "Menno",
                Date = DateTime.Now
            };

            myBlog.Posts.Add(p);
            myBlog.Posts.Add(r);

            String fileName = "blog.db";
            using (DataModelContainer container = FileManager.CreateNew(fileName, overWrite:true))
            {
                container.Blogs.AddObject(myBlog);
                container.SaveChanges();
            } //container is now disposed
            

            using (DataModelContainer container = FileManager.OpenFile(fileName))
            {
                Blog retrieved = container.Blogs.First();
                foreach (Post post in retrieved.Posts)
                {
                    Console.WriteLine(post.Author);
                    Console.WriteLine(post.Date);
                    Console.WriteLine(post.Content);
                    if (post is Review)
                    {
                        Console.WriteLine((post as Review).Rating);
                    }
                }
            }            
        }
    }
}