Storing Large Binary Data (e.g. VARBINARY(MAX)) in SQL Server CE and EntityFramework

Unlike full-blown SQL Server, SQL Server CE (Compact Edition) restricts VARBINARY data types to a maximum size of roughly 8k. Fortunately, there’s an easy enough hack to get around this. According to the MSDN, SQL Server CE supports image data types up to a size of up to 1GB. Since images are just a specific binary format, they’ll work for any other binary data type. As such, all you have to do is decorate the desired model property as so:

[Column(TypeName = "image")]
public byte[] Data { get; set; }

Unfortunately, there’s also a bug with EntityFramework not properly detecting the maximum possible size for SQL Server CE. In order to get around this, you must also add the following code to your data context:

public class MyDbContext : DbContext
    public DbSet<DataClass> Data { get; set; }

    protected override bool ShouldValidateEntity(DbEntityEntry entityEntry)
        //Replace "DataClass" with the class that needs to store large data types
        if (entityEntry.Entity is DataClass)
            return false;
        return base.ShouldValidateEntity(entityEntry);

Thanks to Charles at StackOverflow and Erik for posting the solution to this frustrating problem.


How to not hardcode AspNetDevelopmentServerHost attribute when unit testing

By default, whenever you have VS2010 generate a new unit test for a web method (an MVC controller in my case), it creates a hard-linked file path in the "AspNetDevelopmentServerHost" attribute. This may work fine if you’re the sole developer and code on only one machine, but what about when you work on a team or have multiple machines that will invariably have different file structures? MS recommends using "%PathToWebRoot%", but at least in my case, it always resolves to the default Visual Studio project folder in your User directory. Thankfully, you can also use the "$(SolutionDir)" macro, which should resolve to your project directory, wherever that may be. Thanks to Jason Skowronek for that tip.


“The property is read only” Error Using RIA Services

If you’re trying to modify the property of an object automagically generated from RIA Services within a Silverlight application and you’re getting a runtime error indicating that the the property is read only, here’s a simple fix. Simply add the attribute “[Editable(true)]” from System.ComponentModel.DataAnnotations at the top of the field that you’d like to be editable.

Is it just me or are the number of attributes needed to be placed before each property getting a little ridiculous?

Invalid object name ‘’ using MVC3 and SQL Server

When working through the Movies tutorial on the ASP.NET website, I decided that it’d be fun to try to switch the data provider from SQL Compact to the full version of SQL Server 2008. In preparation for the move, I created a new (blank) database and gave the appropriate permissions to my test account. However, after altering my connection string to accommodate SQL Server 2008, I received the following message:

Invalid object name ‘dbo.Movies’.

At first, I thought that my connection string was off, but it turns out that for Entity Framework to automagically create database tables and relationships, the database must not already exist. As such, simply deleting the blank database completely fixed the problem. This, of course, means that your SQL Server user must have db create privileges, which is something that I don’t grant my test account. To get around this, use more powerful credentials for the initial connection that creates the database. Afterwards, you can switch to a more restricted login.


Canceling AutoPostBack for Asp.Net DropDownList

In my most recent project, I decided to prompt the user before doing a post back when a DropDownList changed. In order to do so, you need to add an “onchange” event to your DropDownList. It should look something like:

onclick="if(!SomeFunction(this)) return false;"

Some Notes:

  • “SomeFunction” is a javascript function that returns true or false based on your criteria.
  • On first inspection, I thougth that I could just call the funciton without wrapping it in what apppears to be a useless IF statement. However, for whatever reason, this IF statement is necessary to cancel the automatic postback.