Skip to content

Instantly share code, notes, and snippets.

@tdshipley
tdshipley / static_c_types_c#.cs
Last active June 1, 2016 16:27
User and Compiler Defined Types in C#
// User defined type
int myUserDefinedTypeNumber = 1;
string myUserDefinedTypeString = "Hello";
bool myUserDefinedTypeBool = true;
// Compiler defined type
var myCompilerDefinedTypeNumber = 1;
var myCompilerDefinedTypeString = "Hello";
var myCompilerDefinedTypeBool = true;
@tdshipley
tdshipley / using_example_c#_forensics.cs
Created June 1, 2016 16:00
Example of a using statement to ensure an object is garbage collected in C#
using (var file = File.OpenRead(CARD_ROOT_PATH + "card.raw"))
{
// Code which uses the file object
}
@tdshipley
tdshipley / asp_core_project_ignore_node_modules.json
Created May 23, 2016 14:34
Ignore node modules in a ASP.NET Core CR2 project
{
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"compile": {
"exclude": [ "node_modules" ]
}
},
"tools": {
"SomeToolsRemovedForBrevity": true
@tdshipley
tdshipley / startup_class_configureservices_example.cs
Last active April 25, 2016 08:48
Example of how to add EF 7 for SQLite to ASP.Net Core startup class
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
// Add SQLite DB using EF Core
var path = PlatformServices.Default.Application.ApplicationBasePath;
var connection = $"Filename={Path.Combine(path, "mydb.db")}";
@tdshipley
tdshipley / cascade_delete_efcore_example.cs
Created April 24, 2016 18:48
Example of setting up cascade delete in EF Core using Fluent API
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata;
public class MyDBContext : DBContext
{
public DbSet<Blog> Blog { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@tdshipley
tdshipley / .gitconfig
Last active April 22, 2016 16:09
My colour settings for git when using a terminal with a dark background
# Colour settings best suited to a terminal with a dark background
[color "status"]
added = white green bold
changed = yellow reverse
untracked = white red bold
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
@tdshipley
tdshipley / panel_bs3_example.html
Created April 22, 2016 15:42
An example of how to create a panel in boostrap sized within a grid.
<!-- http://getbootstrap.com/components/#panels -->
<div class="row">
<div class="col-md-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Panel title</h3>
</div>
<div class="panel-body">
Panel content
</div>
@tdshipley
tdshipley / ef7_sqlite_project.json
Created March 24, 2016 13:08
Sample project.json with the dependencies needed for a ASP.NET Core project that uses EF 7 and ASP.NET Core
{
"dependencies": {
"EntityFramework.Sqlite": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc1-final"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel --server.urls=http://*:9000/",
"ef": "EntityFramework.Commands"
}
@tdshipley
tdshipley / ef_7_error_db_context.cs
Created March 22, 2016 13:38
EF 7 - Error on initial migration db context
using Microsoft.Data.Entity;
namespace CodingMonkey.Models
{
public class CodingMonkeyContext : DbContext
{
public DbSet<Exercise> Exercises { get; set; }
public DbSet<ExerciseTemplate> ExerciseTemplates { get; set; }
public DbSet<ExerciseCategory> ExerciseCategories { get; set; }
public DbSet<Test> Tests { get; set; }
@tdshipley
tdshipley / ef7_inital_migration_stacktrace
Created March 22, 2016 13:36
The stacktrace of the error thrown by EF7 on initial migration.
λ dnx ef migrations add Initial
System.InvalidOperationException: The property 'ExerciseTemplateId' cannot exist on entity type 'object' because the property is not marked as shadow state and no corresponding CLR property exists on the underlying type.
at Microsoft.Data.Entity.Metadata.Internal.Property.set_IsShadowProperty(Nullable`1 value)
at Microsoft.Data.Entity.Metadata.Internal.InternalPropertyBuilder.Shadow(Nullable`1 isShadowProperty, ConfigurationSource configurationSource)
at Microsoft.Data.Entity.Metadata.Internal.InternalPropertyBuilder.Attach(InternalEntityTypeBuilder entityTypeBuilder, ConfigurationSource configurationSource)
at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.PropertyBuildersSnapshot.Attach(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.HasBaseType(EntityType baseEntityType, ConfigurationSource configurationSource)
at Microsoft.Data.Entity.Metadata.Conventions.Internal.DerivedT