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
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace POCOWithSQLiteDemo.Model
{
    public partial class Blog
    {
        #region Primitive Properties
    
        public virtual long Id
        {
            get;
            set;
        }
    
        public virtual string Name
        {
            get;
            set;
        }

        #endregion
        #region Navigation Properties
    
        public virtual ICollection<Post> Posts
        {
            get
            {
                if (_posts == null)
                {
                    var newCollection = new FixupCollection<Post>();
                    newCollection.CollectionChanged += FixupPosts;
                    _posts = newCollection;
                }
                return _posts;
            }
            set
            {
                if (!ReferenceEquals(_posts, value))
                {
                    var previousValue = _posts as FixupCollection<Post>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupPosts;
                    }
                    _posts = value;
                    var newValue = value as FixupCollection<Post>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupPosts;
                    }
                }
            }
        }
        private ICollection<Post> _posts;

        #endregion
        #region Association Fixup
    
        private void FixupPosts(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Post item in e.NewItems)
                {
                    item.Blogs = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Post item in e.OldItems)
                {
                    if (ReferenceEquals(item.Blogs, this))
                    {
                        item.Blogs = null;
                    }
                }
            }
        }

        #endregion
    }
}