Skip to content

Instantly share code, notes, and snippets.

View mikecole's full-sized avatar

Mike Cole mikecole

View GitHub Profile
@mikecole
mikecole / eager-loading-examples
Last active December 23, 2015 13:19
Eager Loading - More Examples
var posts = context.Posts
.Include(p => p.Author.JobTitle) //We are going two levels deep
.ToArray();
//We are going two levels deep here, also. However, since Authors.Posts is a collection, we
//need to do a little LINQ-Fu to properly include Categories.
var author = context.Authors
.Include(a => a.Posts.Select(p => p.Category))
.Single(a => a.ID == id);
@mikecole
mikecole / kick
Created September 24, 2013 18:18
Kick
kick
@mikecole
mikecole / schema-before
Created October 6, 2013 22:38
Schema using default Entity Framework Code First conventions.
CREATE TABLE [dbo].[Authors](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NULL,
[TwitterHandle] [nvarchar](max) NULL,
[JobTitle_ID] [int] NULL,
CONSTRAINT [PK_dbo.Authors] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
@mikecole
mikecole / data-context-with-configurations
Created October 6, 2013 23:31
Data Context w/ Custom Configuration Classes
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions;
using EntityFrameworkExtraMile.Web.Domain.Model;
namespace EntityFrameworkExtraMile.Web.DataAccess
{
public class BlogContext : DbContext
{
public BlogContext()
@mikecole
mikecole / schema-after
Created October 6, 2013 23:34
Schema after custom configuration classes
CREATE TABLE [dbo].[Author](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[TwitterHandle] [nvarchar](50) NULL,
[JobTitleID] [int] NULL,
CONSTRAINT [PK_dbo.Author] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
@mikecole
mikecole / data-annotations
Created October 6, 2013 23:58
Customization with Data Annotations
[Table("Author")]
public class Author : EntityBase
{
public Author()
{
Posts = new Collection<Post>();
}
[Required]
[StringLength(50)]
@mikecole
mikecole / default-automatic-migrations-configuration-class
Created October 29, 2013 15:56
Default Automatic Migrations Configuration Class
namespace EntityFrameworkExtraMile.Web.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<EntityFrameworkExtraMile.Web.DataAccess.BlogContext>
{
public Configuration()
@mikecole
mikecole / entity-framework-initial-code-based-migration
Last active December 26, 2015 21:59
Entity Framework Initial Code-Based Migration
namespace EntityFrameworkExtraMile.Web.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class InitialCreate : DbMigration
{
public override void Up()
{
CreateTable(
@mikecole
mikecole / add-email-to-author
Created October 29, 2013 19:49
Entity Framework Code-Based Migrations AddEmailToAuthor
namespace EntityFrameworkExtraMile.Web.Migrations
{
using System.Data.Entity.Migrations;
public partial class AddEmailToAuthor : DbMigration
{
public override void Up()
{
AddColumn("dbo.Author", "Email", c => c.String(maxLength: 50));
}
@mikecole
mikecole / entity-framework-etc-code-based-migration
Created October 29, 2013 20:12
Entity Framework Etc. Code-Based Migration
namespace EntityFrameworkExtraMile.Web.Migrations
{
using System.Data.Entity.Migrations;
public partial class InitializeEtc : DbMigration
{
public override void Up()
{
CreateIndex("Category", "Name", true);
CreateIndex("JobTitle", "Title", true);