1 2 3 4 5 6 7 8 9 10 11 12

ASP.NET Identity 1.0 - Database First

16/08/2014

Note: This tutorial applies to Identity 1.0 which is the previous version of ASP.NET Identity. I have a newer post on the same subject here

If you've read my previous posts you probably know I'm working on some EF Database first stuff, this seems to be the least popular way to use EF but to me seems like a common business use-case for it.

The MVC template in Visual Studio comes with the new Asp.NET Identity system built in, but it's designed to be used with Code First. This leads to people asking how to use Identity with Database First? The suggested soultion is to use the table structure provided when you use Code First and port it to your pre-existing database.

This seemed like an unpleasant approach to me (and a lot of work), especially because I think the database layout for those tables is kinda sucky. This is when it occurred to me that we're basically trying to implement Identity without using what the documentation thinks of as Entity Framework.

When you Google how to do that, you get this great post from Mark Johnson. You can follow his approach and just substitute his use of Dapper for use of Entity Framework. for instance:

public Task CreateAsync(User user)
{
    if (user == null)
    {
        throw new ArgumentNullException("user was null");
    }

    return Task.Factory.StartNew(() =>
        {
            db.Users.Add(user);
            db.SaveChanges();
        });
}

public Task DeleteAsync(User user)
{
    User checkUser = db.Users.Find(user.Id);

    if (user == null || checkUser == null)
    {
        throw new ArgumentNullException("user was null");
    }

    return Task.Factory.StartNew(() =>
    {
        db.Users.Remove(user);
        db.SaveChanges();
    });
}

Where db is your DbContext. Hopefully this helps you as much as it helped me, it's a far nicer solution than rolling your own Auth system.

...

AOB (1)

10/08/2014

AOB here is used to mean 'any other business' rather than the Swedish pop group Ace of Base or the Accessory olfactory bulb.

This a blog post to summarise a few things which don't make a whole blog post in their own right.

MarkdownPad 2


For those of you who don't know, markdown is readable HTML syntax so you can write properly structured HTML in a human friendly way.

The conversion to HTML is then performed by the markdown library in your language of choice using a mega-regex.

The format is used by sites like StackOverflow and GitHub for comments/text and I also use it for my blog posts.

So after Scott Hanselmann mentioned it in his blog I downloaded MarkdownPad 2.

Beforehand I was using Notepad++ to write Markdown and it was horribly long winded due to the poor choice of application. Now writing Markdown is about as easy as using Word (so go download it if you use markdown).

...

Metadata For Entity Framework MVC - Database First

09/08/2014

The model validation in MVC is brilliant but for best usage requires Attributes to be added to your model.

If you use Entity Framework to generate your model you are given classes which will be overwritten when you regenerate the model as seen in the warning automatically appended to your file:

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

The way around this is to use a Metadata Class. Because an Entity Framework model class is defined as a partial class you can extend it.

For the following model class:

namespace Project.EntityFramework
{
    using System;
    using System.Collections.Generic;

    public partial class Dog
    {
        public Dog()
        {
            this.Collars = new HashSet<Collar>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Collar> Collars { get; set; }
    }
}

You can extend the partial class as long as your class is in the same namespace. The specific metadata file is shown:

using System;
using System.ComponentModel.DataAnnotations;

namespace Project.EntityFramework
{
    [MetadataType(typeof(Dog.DogMetadata))]
    public partial class Dog
    {
        internal sealed class DogMetadata
        {
            #pragma warning disable 0649
            private DogMetadata() { }

            [Editable(false)]
            public object Id { get; set; }

            [Display(Name = "Woofy Name")]
            [Required(ErrorMessage = "Name Is Required", AllowEmptyStrings = false)]
            [MinLength(10)]
            public object Name;

            [Editable(false)]
            public object Collars { get; set; }
        }
    }
}

This Metadata file Dog.Metadata.cs is in the same folder and namespace (folder is not always equal to namespace but by default in visual studio) as the model DatabaseModel.edmx. Metadata files have .metadata in the name by convention.

The fact metadata is being used is declared with the attribute:

[MetadataType(typeof(Dog.DogMetadata))]

Where the type is the metadata class you're using. Therefore it is possible to define the metadata class separately in another file but it's best to keep everything together.

...

jQuery Fit Footer To Window Height

08/08/2014

Sometimes your content just doesn't fit the window height, for exampling when you're styling a page with placeholder content. Or, for a short post like this.

That's why I use the script below for making sure my footer appears at the bottom of pages where the content is shorter than the window:

function AdjustWindowHeight()
{
    var requiredHeight = $(window).height() - $("#footer").height();
    $('#wrap').css('min-height', requiredHeight);
}

$(window).on("resize", AdjustWindowHeight);

$(function () {
    AdjustWindowHeight();
});

Or the minified version:

function F(){var a=$(window).height()-$("#footer").height(); $("#wrap").css("min-height",a)}$(window).on("resize",F);$(function(){F()});

All you need is a div containing all your footer content and and wrap div containing all other content:

<body>
<div id="wrap">Content</div>
<div id="footer">Footer :)</div>
</body>
...

JSON.NET

05/08/2014

JSON.NET by James Newton-King is the library for working with JSON in .NET. The following is a small guide for using JSON.NET. It is in no way a substitute for the full documentation.

To follow along obtain the JSON.NET package using NuGet and the Newtonsoft.Json dll will be added to your project's references. Alternatively download from the official website and add the reference manually.

Serialize An Object And De-serialize

Serialization is the process of translating data structures or object state into a format that can be stored - Wikipedia.

We will start our investigation with a very simple C# object and continue from there. As always, we are using the Dog class:

public class Dog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Breed { get; set; }
    public DateTime Birthday { get; set; }

    public int CalculateAgeInDays()
    {
        return (DateTime.Now - Birthday).Days;
    }
}

This is very simple class but might mirror something you need to serialize.

To start, let's create a dog and convert it to a string of Json (which I'll stop capitalising because it's a pain to type).

Dog dog = new Dog
{
    Id = 4,
    Breed = "Labradoodle",
    Name = "Baron Von Lassie",
    Birthday = Convert.ToDateTime("2013-01-07"),
};
string json = JsonConvert.SerializeObject(dog);

The string that we obtain is 'minified':

{"Id":4,"Name":"Baron Von Lassie","Breed":"Labradoodle","Birthday":"2013-01-07T00:00:00"}

As you can see all extraneous whitespace and extra line-breaks have been removed. This is ideal for data transfer objects such as a response from a Web Service, however if we're trying to present our data in a human readable way it's nicer to set Formatting.Indented like so:

...
1 2 3 4 5 6 7 8 9 10 11 12