Category: C#

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.

HTML Placeholder not working in IE9 and earlier!!

Recently found this very useful jQuery plugin for fixing placeholder issues in old IE browsers. It seems to cover most scenarios and is very easy to  implement 🙂

https://github.com/mathiasbynens/jquery-placeholder

Just add the script after the jQuery and call

$('input, textarea').placeholder();

You will also need to add the placeholder style in your css.

/* Placeholder fix for IE9 for use with jQuery.placeholder plugin */
 .placeholder { color: #aaa; }

If you are a c# razor person, something like this on your impacted pages will do the trick (assuming you something like

@RenderSection("MyJavaScript", false)

in your layout).

@section MyJavaScript
 {
 <script src="@Url.Content("~/Scripts/jquery.placeholder/jquery.placeholder.js")" type="text/javascript"></script>
 <script>
 $('input, textarea').placeholder();
 </script>
 }

How to make a short URL friendly GUID in C#

Been trying my own hack to make a short GUID for a web form submission, but hit issues with incompatible characters. Just before making more changes I came across this excellent blog and code to easily convert GUIDs to Short GUIDs and back.

I have found this especially useful when sending unique vendor transaction refs to the SagePay forms service as they have a 40 character limit.

http://www.singular.co.nz/blog/archive/2007/12/20/shortguid-a-shorter-and-url-friendly-guid-in-c-sharp.aspx

.Net MVC 3 Logon does not redirect out the box–quick fix!

It seems that the templates for MVC 3 razor are not quite complete. If you attempt to browse to a page where authentication is required, then you will be redirect to the Logon page with a ReturnUrl query string.

Something like:

http://mysite.com/Account/LogOn?ReturnUrl=%2fThePageIWant

That’s great except, out the box, the Logon page does not make use of this and the destination page is not redirected to after a successful login. The fix is easy.

Add the returnUrl route value into the Html.BeginForm method as shown below.

Html.BeginForm("LogOn", "Account", new { returnUrl = Request.QueryString["ReturnUrl"] })

You should already have some code in the controller that does some sanity checking on the returnUrl before doing the redirect. Somthing like:

if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
       && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
    return Redirect(returnUrl);
}

Make sure this is after the SetAuthCookie line!

How to get ‘Create Unit Test’ shortcut back in Visual Studio 2013

Why the hell Microsoft thought it was a good idea to remove this I have no idea. If they want good reliable code on their platforms you would have thought this would be good to leave in to encourage developers to create unit tests!

Anyway, rant over, this blog explains all the options. The last one being the first one to try!

http://serena-yeoh.blogspot.com.es/2013/02/visual-studio-2012-create-unit-test.html

TimeSpan to String Converter for XAML binding

Need to convert a TimeSpan to a sensible string for you Windows Phone or Windows Store App. With a little Bing help I created this converter. I hope you find it useful.


public class TimeSpanToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        TimeSpan span = (TimeSpan)value;
        if (span == TimeSpan.MinValue)
        {
            return "0 seconds";
        }
        string formatted = string.Format("{0}{1}{2}{3}",
            span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty,
            span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty);
        if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);
        if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds";
            return formatted;
    }
        
    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        string strValue = value as string;
        TimeSpan resultSpan;
        if (TimeSpan.TryParse(strValue, out resultSpan))
        {
            return resultSpan;
        }
        throw new Exception("Unable to convert string to date time");
    }
 }