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.


“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?

Row not found or changed & LINQ

This is a particularly well-known error (see here, here, and here), but this is the second time that I’ve had to do a search on this error so I thought that I’d post it here so that I can find it more quickly. Anyway, when this error occurs, make sure that the NULL types in the LINQ dbml file match your SQL schema NULL types. Mismatches between the two seem to be a common cause of this error.


SaveFileDialog Error When Debugging Silverlight Applications

If you’re having difficulties debugging a section of code that uses the SafeFileDialog.ShowDialog() method, the reason is that Silverlight requires that the ShowDialog() method be called from a user-generated action. For whatever reason, entering into debug mode makes Silverlight think that you are not responding to a user-action, even when you are. To work around this, simply place the breakpoint after you call the ShowDialog() method.


Extending a UserControl in Silverlight

In a current project that I’m working on, I needed to extend a UserControl to add some additional specialized functionality. This is very easy to do if all you need is to extend code, but it becomes much more difficult if you want to extend it via a XAML/C# UserControl. After much web searching, I found a very simple and elegant solution, thanks to this site. Basically, all you have to do is modify the root element of your extended file as follows:


Where SomeBaseTypeYouWantToUse is the class that you’d like to extend. Simple!


Update (6/3/2010):I ran into some weird problems with this hack that I was unable to resolve. If anyone ends up reading this, please let me know if this worked correctly for your application.