Skip to content

Instantly share code, notes, and snippets.

@dnasca
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save dnasca/2cf61e8ca29c702ada29 to your computer and use it in GitHub Desktop.

Select an option

Save dnasca/2cf61e8ca29c702ada29 to your computer and use it in GitHub Desktop.
Creating a Data layer - 2.) The Database Context - Inheriting from DbContext
/*
Entity framework expects a Context class. This class is used to communicate between the code and the dB. Since we built our model
classes by convention, we won't need to do much in the way of configuration of our context.
This is the pipeline to all queries, inserts, updates, deletes for any data operation is going to be passed through.
*The next class we'll build will be the NoteRepository class to wrap the context in.
*/
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Note.Data
{
public class NoteContext : DbContext
{
//constructor is required
public NoteContext()
: base("DefaultConnection") // connectionString
{
// TO DO: Implement migrations here
}
// setup properties on this context to represent the tables to be stored in the dB
// Entity will infer by the structure of these classes on how to build our dB
public DbSet<Topic> Topics { get; set; }
public DbSet<Reply> Replies { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment