Skip to content

Instantly share code, notes, and snippets.

View aadamsx's full-sized avatar

Aaron Adams aadamsx

View GitHub Profile
@aadamsx
aadamsx / EF-EnumCodeFirst
Last active December 22, 2015 11:19
Entity Framework 6 enum support example
using System.Data.Entity;
// In Entity Framework, an enumeration can have the following underlying types: Byte, Int16, Int32, Int64 , or SByte.
public enum DepartmentNames
{
English,
Math,
Economics
}
@aadamsx
aadamsx / EF-RawSQLQueries
Last active December 22, 2015 12:09
Entity Framework allows you to query using LINQ with your entity classes. However, there may be times that you want to run queries using raw SQL directly against the database. This includes calling stored procedures, which can be helpful for Code First models that currently do not support mapping to stored procedures. The techniques shown in thi…
// The SqlQuery method on DbSet allows a raw SQL query to be written that will return entity instances. The returned objects // will be tracked by the context just as they would be if they were returned by a LINQ query. For example:
using (var context = new BloggingContext())
{
var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();
}
/*
Note that, just as for LINQ queries, the query is not executed until the results are enumerated—in the example above this is done with the call to ToList.
Care should be taken whenever raw SQL queries are written for two reasons. First, the query should be written to ensure that it only returns entities that are really of the requested type. For example, when using features such as inheritance it is easy to write a query that will create entities that are of the wrong CLR type.
@aadamsx
aadamsx / EF-Async-CreateModel
Created September 6, 2013 22:42
This walkthrough will get you started with creating an application that makes use of the Entity Framework’s new asynchronous methods, which were first introduced in the EF6 alpha1 release. While not all applications may benefit from asynchrony, it can be used to improve client responsiveness and server scalability when handling long-running, net…
// Create the Model
/*
For simplicity’s sake, we’ll begin by setting-up a C# console application for managing a database of questions and answers, called “AnswersDB.” Each question can have multiple answers, and each answer belongs to only one question. We’ll be using EF’s Code First workflow to create our model and generate the database.
Here is the model that powers this Q&A service:
*/
public class Question
{
@aadamsx
aadamsx / WebApi-UT-HTTPGet
Created September 6, 2013 23:06
Writing Unit Tests for ASP.NET Web API Controller
/*
Unit tests for a ASP.NET Web API controller in the EFMVC reference application. Let me introduce the EFMVC app, If you haven't heard about EFMVC. EFMVC is a simple app, developed as a reference implementation for demonstrating ASP.NET MVC, EF Code First, ASP.NET Web API, Domain-Driven Design (DDD), Test-Driven Development (DDD). The current version is built with ASP.NET MVC 4, EF Code First 5, ASP.NET Web API, Autofac, AutoMapper, Nunit and Moq. All unit tests were written with Nunit and Moq. You can download the latest version of the reference app from http://efmvc.codeplex.com/
*/
// Unit Test for HTTP Get
/*
Let’s write a unit test class for verifying the behaviour of a ASP.NET Web API controller named CategoryController. Let’s define mock implementation for Repository class, and a Command Bus that is used for executing write operations.
*/
@aadamsx
aadamsx / EF-FluentApi
Created September 7, 2013 01:13
When working with Entity Framework Code First the default behavior is to map your POCO classes to tables using a set of conventions baked into EF. Sometimes, however, you cannot or do not want to follow those conventions and need to map entities to something other than what the conventions dictate. There are two main ways you can configure EF to…
Introduction
The code first fluent API is most commonly accessed by overriding the OnModelCreating method on your derived DbContext. The following samples are designed to show how to do various tasks with the fluent api and allow you to copy the code out and customize it to suit your model, if you wish to see the model that they can be used with as-is then it is provided at the end of this article.
Property Mapping
The Property method is used to configure attributes for each property belonging to an entity or complex type. The Property method is used to obtain a configuration object for a given property. The options on the configuration object are specific to the type being configured; IsUnicode is available only on string properties for example.
@aadamsx
aadamsx / EF-CodeFirstConventions
Created September 7, 2013 01:14
Code First enables you to describe a model by using C# or Visual Basic .NET classes. The basic shape of the model is detected by using conventions. Conventions are sets of rules that are used to automatically configure a conceptual model based on class definitions when working with Code First. The conventions are defined in the System.Data.Entit…
Type Discovery
When using Code First development you usually begin by writing .NET Framework classes that define your conceptual (domain) model. In addition to defining the classes, you also need to let DbContext know which types you want to include in the model. To do this, you define a context class that derives from DbContext and exposes DbSet properties for the types that you want to be part of the model. Code First will include these types and also will pull in any referenced types, even if the referenced types are defined in a different assembly.
If your types participate in an inheritance hierarchy, it is enough to define a DbSet property for the base class, and the derived types will be automatically included, if they are in the same assembly as the base class.
In the following example, there is only one DbSet property defined on the SchoolEntities class (Departments). Code First uses this property to discover and pull in any referenced types.
public class SchoolEntities : DbContext
{
// Node.js CheatSheet.
// Download the Node.js source code or a pre-built installer for your platform, and start developing today.
// Download: http://nodejs.org/download/
// More: http://nodejs.org/api/all.html
// 0. Synopsis.
// http://nodejs.org/api/synopsis.html
## SYNTAX:
var pattern = new RegExp(pattern, attributes); # attributes: g (global); i (case-sensitive); m (multiline matches)
var pattern = /pattern/attributes; # same as above
## BRACKETS:
[...]: Any one character between the brackets.
[^...]: Any one character not between the brackets.
// Date Object CheatSheet
// The Date object is used to work with dates and times.
// More: http://www.w3schools.com/jsref/jsref_obj_date.asp
// 1. Instantiating a Date.
var date = new Date();
var date = new Date(milliseconds);
# Node-WebKit CheatSheet
# Download: https://github.com/rogerwang/node-webkit#downloads
# Old Versions: https://github.com/rogerwang/node-webkit/wiki/Downloads-of-old-versions
# Wiki: https://github.com/rogerwang/node-webkit/wiki
# How: https://github.com/rogerwang/node-webkit/wiki/How-node.js-is-integrated-with-chromium
# 1. Run your application.
# https://github.com/rogerwang/node-webkit/wiki/How-to-run-apps