Monday, April 23, 2012

Fluent NHibernate: Cannot create an instance of AutoMapping X because Type.ContainsGenericParameters is true

This happens probably because you forgot to tell Fluent NHibernate which classes to map explicitly (i.e. its trying to map a class that isn’t one of your entity models).
I usually filter my Fluent NHibernate models overriding the DefaultAutomappingConfiguration:
public class AutoMappingConfig : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        return typeof(EntityBase).IsAssignableFrom(type);
    }
}

And tell Fluent NHibernate about it:
Fluently.Configure()
    .Database(databaseConfig)
    .Mappings(
        m =>
        m.AutoMappings.Add(
            AutoMap.AssemblyOf<User>(new AutoMappingConfig()));

Wednesday, February 22, 2012

XAML Control to Control Binding

I have a control that I want to enable/disable based on a checkbox.  I ended up using a binding with ElementName and Path:

<CheckBox 
x:Name="Enabler" />

<ComboBox
IsEnabled="{Binding ElementName=Enabler,Path=IsChecked}" />

Tuesday, February 21, 2012

Add a ToolTip in Code

Today I had to create many controls in code (not XAML).  I wanted to add tooltips as I created them:
void AddManyControls()
{
    var control = new Control();
    ToolTip toolTip = new ToolTip 
    { 
        Content = "ToolTip Content"
    }

    ToolTipService.SetToolTip(control, toolTip); 
}

Monday, February 20, 2012

ASP.NET Restarting On Every Request

It turns out I was writing some temporary files to a subfolder in the “bin” (or application) directory.  The server saw that there were changes, and (correctly) decided a restart was required.

Here are some other options on why the server (both WebDev and IIS/IISExpress) may be restarting/recycling too much: http://blogs.msdn.com/b/johan/archive/2007/05/16/common-reasons-why-your-application-pool-may-unexpectedly-recycle.aspx

Sunday, February 19, 2012

Reload fstab Without a Reboot

Found this nice post: http://chrisschuld.com/2007/08/reload-fstab-etcfstab/

After you edit the /etc/fstab, use mount -a.

Add a VirtualBox Disk to Linux

I've started to play around with linux in VirtualBox.  I'm going to start posting things I find useful, since I'm a Windows man by trade.  This is the first:

I was adding a new VirtualBox disk for my data (so I can keep the OS/programs seperate from my personal stuff).  Here's how I did it.
  1. Create the disk in the VirtualBox GUI.
  2. Format the disk in Linux: 
    1. Find the disk (probably /dev/sdb or some such path): fdisk -l
    2. Format the disk: fdisk /dev/sdb
  3. Edit the fstab to mount the disk where you want it:
    • /dev/sdb /home/david                                ext2    defaults        0 0
    • Note that I mounted to my home directory, so I need to move my existing home dir (which has all the crappy Windows type folders) and then create it again so that the mount can find the folder.
  4. Remount: mount -a
  5. EDIT 2012-02-19: Copy all of the .* files from the old user dir to the new one.
  6. Done! (I think, I'm new at this)

Wednesday, October 19, 2011

Ensure Correct Ring Orientation for SqlGeography

SqlGeography is very picky about the order of points when reading Well Known Text (WKT): if your WKT points aren’t just so, the de-serialization will throw something like this:

A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":
Microsoft.SqlServer.Types.GLArgumentException: 24205: The specified input does not represent a valid geography instance because it exceeds a single hemisphere. Each geography instance must fit inside a single hemisphere. A common reason for this error is that a polygon has the wrong ring orientation.

Luckily, there are smarter people in the world than me.  Here’s a post on how to use T-SQL to ensure correct ring orientation.  Here’s how I do it using Microsoft.SqlServer.Types in C#:

// a helper SqlGeometry, which isn't picky about ring orientation
var geometryHelper =
SqlGeometry.STGeomFromText(wkt.ToSqlChars(), srid).MakeValid();

// STUnion will automagically correct any bad ring orientation
var validGeom =
geometryHelper.STUnion(geometryHelper.STStartPoint());

// use the validGeom with correct ring orientation to
// create a SqlGeography.
var validGeography =
SqlGeography.STGeomFromText(validGeom.STAsText(), srid);