Category: C#

Azure Active Directory Graph API Wrapper to help make it a bit easier!

I have recently been trying to program against the Azure Active Directory (AAD) using the Microsoft.Azure.ActiveDirectory.GraphClient library. Unfortunately this library literally has no useful comments to assist understanding  or clarify parameters etc.. Let alone how best to use or implement objects and methods or what and why exception may occur.

Equally the MSDN documentation seems to be lacking in any examples and really has minimal comments (although I see it’s getting a bit better…I think).

To this end I have created a ‘wrapper / handler’ to simplify all sorts of AAD interactions called AADGraphHandler. It effectively will help manage the creation of the ActiveDirectoryClient and a bunch of it’s operations. You can find this on GitHub at https://github.com/nrogoff/AADGraphHandler

You can get access to the ActiveDirectoryClient directly, and so any methods not covered yet. (e.g. Adding and removing roles to a user. Just not needed it yet!)

Continue reading “Azure Active Directory Graph API Wrapper to help make it a bit easier!”

Unit Test Class ReSharper Template and Snippets for nUnit and MS Test

Quick helper boiler plates for setting up your Unit Test.

I have included ReSharper Template here that you can import directly Resharper Templates. Also thanks you to Alessandro Aeberli for making the these Visual Studio Snippets

After importing the ReSharper Templates then you can use ‘uTestBoiler’ or ‘uTestnUnitBoiler’ shortcuts to add the code below into any test class.

If you want to make your own snippets you can use the Visual Studios Snippet Manager by following the instructions here https://msdn.microsoft.com/en-us/library/ms165394.aspx

Continue reading “Unit Test Class ReSharper Template and Snippets for nUnit and MS Test”

Bulk update EXIF ‘Shot Taken Date’ on JPG photos using ‘ImageProcessing Console’.

If, like me, you have digital photo’s stretching back to the early days of digital cameras and even scans of film and slides, then you may have the same issue I had attaching the right date to the file. I have over 35,000 digital images, so developed a little command line tool to help called ‘Image Processing Console‘ (https://github.com/nrogoff/ImageProcessingConsole).

Continue reading “Bulk update EXIF ‘Shot Taken Date’ on JPG photos using ‘ImageProcessing Console’.”

Entity Framework – Re-querying or fetching latest foreign key relationship records

In entity framework, you can often include foreign key or relationship collections in your LINQ request by using the ‘Include’ statement.

For example you may want to get a collection of Customers and their related Invoices. This would look something like:

var customersWithInvoices = DataContext.Customers.Include("Invoices")

However there may be times where this is not possible or you want to ensure that you have the latest related collection or you only want to fetch that collection when you need it.

To do this you need when the related records are a collection and re-query the navigation property for your parent entity (here ‘customer’) we use the Collection() method as follows:

try
{
DataContext.Entry(customer)
.Collection<Invoice>("Invoices")
.Query()
.ToList();
}
catch(Exception e)
{
Console.WriteLine(e);
}

Where ‘Invoices’ is the name of your navigation property. Now you can access the latest related objects.

e.g.

var countOfInvoices = customer.Invoices.Count();

To do this you need for a one-to-one related single entity then use the Reference() method instead as follows (Say we wanted the Country dial code of our customers Country):

DataContext.Entry(customer).Reference<Country>("CountryOfResidence").Query().FirstOrDefault();

Here the navigation property is called ‘CountryOfResidence”.

We can now navigate to the Country entity and get the dial code.

e.g.

var dialCode = customer.CountryOfResidence.DialCode;

Getting access to and using Visual Studio code snippets with Resharper installed

When you first install Resharper, your Visual Studio code snippets are imported into Resharpers ‘Live Templates’. This is all very well until you install, update or create new Visual Studio code snippets.

If you have ever wondered how to use the Visual Studio code snippets when you have Resharper installed then here is the quick solution.

Press CTRL+J and then start typing the code snippet shortcut you are after. You will now see the list of available Visual Studio code snippets.

You can find this mentioned here http://www.jetbrains.com/resharper/webhelp/Templates__Applying_Templates__Inserting_Imported_Code_Snippets.html

Entity Framework – Setting integer keys client side gets ‘Cannot insert the value NULL’

I have just been banging my head against a wall trying to get a simple database record added to a simple table using Entity Framework! My table has a primary key field of type INT, but is NOT set as an identity field (i.e. Not set to increment SQL server side). I wanted the ability to set the Id in code.

An example would be a model like:

public Contact
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

then you would normally expect the standard EF code like this to work

var myNewContact = new Contact
{
    Id = 123,
    Name = "John Smith"
}
DataContext.Contacts.Add(myNewContact);
DataContext.SaveChanges();

However it always came back with the a DbUpdateException stating

“Cannot insert the value NULL into column ‘Id’, table ‘MyDatabase.dbo.Contacts’; column does not allow nulls. INSERT fails.”

It turns out that Entity Framework assumes that an integer [Key] field will always be generated server side. Maybe I should have known this, but actually found it very hard to locate this bit of information.

Once you know this the fix is very straight forward. You just need to decorate your model object with

[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

so the model above becomes:

public Contact
{
    [Key, DatabaseGenerated( DatabaseGeneratedOption.None )]
    public int Id { get; set; }
    public string Name { get; set; }
}

I hope this saves someone wasting as much time and loosing as much hair as I did on this simple issue!

One reason it took so long, is that I can’t get Entity Framework Profiler working with the Azure Emulator. If any has had any success with this, please let me know.