public TodoItemContext CreateContext() { // create the database from scratch if it has not been initialised if (!DatabaseInitialized) { // get the path to the .SDF file var fullPath = GetRealDatabaseFilePath(DatabaseName); // delete it if it already exists if (File.Exists(fullPath)) File.Delete(fullPath); // get the SQL CE connection string string connectionString = GetRealDatabaseConnectionString(DatabaseName); // NEED TO SET THIS TO MAKE DATABASE CREATION WORK WITH SQL CE!!! // Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); using (var context = new TodoItemContext(connectionString)) { context.Database.Create(); } // Set the initialised flag so that we don't re-create the database again for this instance of the class DatabaseInitialized = true; } // create the DbContext with the SQL CE connection string return new TodoItemContext(GetRealDatabaseConnectionString(DatabaseName)); }
public void Dispose(TodoItemContext context) { try { Dispose(); } finally { if (context != null) { context.Dispose(); } } }
public UnitOfWork(TodoItemContext context) { _context = context; }
/// <summary> /// Initiate a new todo list for new user /// </summary> /// <param name="userName"></param> private static void InitiateDatabaseForNewUser(string userName) { TodoItemContext db = new TodoItemContext(); TodoList todoList = new TodoList(); todoList.UserId = userName; todoList.Title = "My Todo List #1"; todoList.Todos = new List<TodoItem>(); db.TodoLists.Add(todoList); db.SaveChanges(); todoList.Todos.Add(new TodoItem() { Title = "Todo item #1", TodoListId = todoList.TodoListId, IsDone = false }); todoList.Todos.Add(new TodoItem() { Title = "Todo item #2", TodoListId = todoList.TodoListId, IsDone = false }); db.SaveChanges(); }
public static void Add2TodoListsAnd3TodoItems(TodoItemContext dbContext) { Add2TodoListsAnd3TodoItems(dbContext.TodoLists, dbContext.TodoItems); }