Skip to content

Getting Started

Todd Thomson edited this page Aug 13, 2018 · 9 revisions

The following code snippets are all taken from the TodoApp Sample

The DataContext

Your application must first create a custom data context by declaring a class that is derived from DataContext.

    /// <summary>
    /// The Todo app data context.
    /// </summary>
    /// <remarks>
    /// Custom data contexts derive from <see cref="DataContext"/>.
    /// </remarks>
    public class TodoDataContext: DataContext
    {
    }

EntitySets

Next, define and add your EntitySets to you DataContext.

    public class TodoDataContext: DataContext
    {
        private static TodoDataContext _dataContext;

        /// <summary>
        /// Gets or sets the Todo items.
        /// </summary>
        /// <remarks>
        /// Entity sets support the <see cref="IQueryable"/> interface.
        /// </remarks>
        public EntitySet<TodoItem> TodoItems { get; set; }

        public TodoDataContext( DataContextOptions options ) : base( options )
        {
            // Add your entity sets to the context in the data context constructor.
            TodoItems = new EntitySet<TodoItem>( this );
        }
    }

Model Building

Entities.Sqlite provides a fluent builder for configuring and building your entity database model. Override the OnModelBuilding method in your data context to get started.

        /// <summary>
        /// Override for configuring entity mapping.
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelBuilding( RelationalModelBuilder modelBuilder )
        {
            modelBuilder.Entity<TodoItem>( entity =>
            {
                entity.ToTable( "Todos" );

                entity.Column( p => p.Id )
                    .IsKey();

                entity.HasIndex( p => p.Name ).Name( "IX_TodoItem_Name" ).IsUnique();

            } );

            base.OnModelBuilding( modelBuilder );
        }

Data Repository

Add your database CRUD methods to a repository service and your done ( well, with your data services! ).

    public class TodoItemDatabase
    {
        readonly TodoDataContext _dataContext;

        public TodoItemDatabase( string dbPath )
        {
            var connectionString = "Data Source=" + dbPath;
            var options = new DataContextOptionsBuilder().UseSqlite( connectionString ).Options;

            _dataContext = new TodoDataContext( options );

            _dataContext.Database.Creator.CreateIfNotExists();
        }

        public Task<List<TodoItem>> GetItemsAsync()
        {
            return _dataContext.TodoItems.ToListAsync();
        }

        public Task<List<TodoItem>> GetItemsNotDoneAsync()
        {
            return _dataContext.TodoItems.Where( i => i.Done == false ).ToListAsync();
        }

        public Task<TodoItem> GetItemAsync( int id )
        {
            return _dataContext.TodoItems.FirstAsync( p => p.Id == 1 );
        }

        public Task<int> SaveItemAsync( TodoItem item )
        {
            if ( item.Id != 0 )
            {
                return _dataContext.TodoItems.UpdateAsync( item );
            }
            else
            {
                return _dataContext.TodoItems.AddAsync( item );
            }
        }

        public Task<int> DeleteItemAsync( TodoItem item )
        {
            return _dataContext.TodoItems.DeleteAsync( item );
        }
    }