No columns when binding to complex type in Entity Framework

The day before yesterday I had my problem with getting my stored proc recognised in the Entity Framework (EF) designer in Visual Studio 2010 (VS2010). Yesterday my next step was to get one of my stored procedures (sprocs) that use Full Text Search (FTS) bound to a complex type. So I could get all the results and aggregations in one round trip to the database. I started with the simplest sproc I could:

CREATE PROCEDURE [dbo].[P_ParentFullTextSearch]
@queryString NVARCHAR(250)
AS
BEGIN
SELECT P.[ID], P.[Name],
(SELECT COUNT(1)
FROM  dbo.[Children] C
WHERE P.[ID] = [C.ParentID]) AS ChildrenCount
FROM   dbo.[Parent] P
WHERE  FREETEXT(P.[Name], @queryString)
AND    P.IsDeleted = 0
END

I granted EXECUTE and CONTROL permissions to the EF connection string SQL Server login on the proc and could get it picked up in the Add Function Import but could not retrieve any column information. Frustrating…

I tried a number of things on the MSDN forums like SET NOCOUNT OFF and adding the VIEW DEFINITION permission. None worked. After running a trace (the first thing I should have done) in the SQL Server Profiler I found clicking Get Column Information was executing:


SET FMTONLY OFF;
SET FMTONLY ON;
EXEC [db_name].[dbo].P_ParentFullTextSearch @queryString=null;
SET FMTONLY OFF;

The problem comes from the fact you CANNOT use nulls in FTS predicates – DOH should have noticed that. This causes the metadata query to fall over. Unfortunately the error you get when running the SQL does not bubble up into the Add Function Import window.

A simple NULL check resolved the issue:

CREATE PROCEDURE [dbo].[P_ParentFullTextSearch]
@queryString NVARCHAR(250)
AS
BEGIN
IF @queryString IS NULL
BEGIN
SELECT    (CAST NULL AS INT) AS [ID],
(CAST NULL AS NVARCHAR(50)) AS [Name],
(CAST NULL AS INT) AS [ChildrenCount]
RETURN
END
SELECT P.[ID], P.[Name],
(SELECT COUNT(1)
FROM dbo.[Children] C
WHERE P.[ID] = [C.ParentID]) AS ChildrenCount
FROM dbo.[Parent] P
WHERE FREETEXT(P.[Name], @queryString)
AND P.IsDeleted = 0
END

Behold! Metadata and a sproc bound to a complex type.

Cannot see stored procedure in Entity Framework model?

Unfortunately I could not see the stored procedure in the list of items to be added when refreshing the model from the database. I have been implementing a stored procedure/inline-table function to facilitate a Full Text Search (FTS) in SQL Server 2008 using ADO .NET Entity Framework (EF).

The cause was that the EF connection did not have Control/Execute permissions on the function/procedure. Grant these permissions to the user utilised in your EF connection string and it will pop-up in the list of stored procedures to be added when you go back to update the model.

Once that is done you can then map the proedure/function results to your entities or a complex type. This is accomplished by selecting Add function import when right-clicking anywhere in the EDMX Designer View or the stored procedure in the Model View.

Run a single unit test in Visual Studio

A colleague of mine found a handy way to run a single unit test in Visual Studio 2010. Go to Test -> Windows -> Test View, select the test you want to run and right-click. Hit Run Test and away you go.

Always remember to run all your tests though before you commit your changes.

AdoMdClient – Where are the measure groups?

Been busy creating a Silverlight/SSAS mashup recently to show off their potential when used together to both colleagues and clients. While attempting to build the measure selection/filtering interface I thought it would be pleasant if they were grouped by their… measure groups, hierarchically like they are when browsing a cube in SSMS or Visual Studio. Measures are easy enough to find as a collection on the CubeDef object as a MeasureCollection. But where are measure groups hiding?

Naturally I would have thought there would be a MeasureGroup or MeasureGroupCollection off the CubeDef – no such luck. Debugging and taking a closer look at the Measure object, in the PropertyCollection (the Properties member) I spyed the property MEASUREGROUP_NAME. The sweet smell of victory. So to get your measure’s measure group here is the C#:

Property measureGroupProperty = measure.Properties["MEASUREGROUP_NAME"];
string measureGroupName = measureGroupProperty.Value.ToString();

WN111 lag spikes

After a seven week fiasco I recently got Virgin Media to install my long awaited 50Mb connection. I was pretty stoked with the speed to say the least and they even provided a Netgear WN111 Wireless-N USB adapter which was handy as my Dell desktop had no wireless hardware.

Not long into the affair, I found a few hours to have a go at some Team Fortress 2 and Dawn of War 2. The love affair quickly became sour. My ping was rather large and hovering around the 80-90ms mark. Not too terrible but in gaming terms I might as well have had one leg chopped off. This was compounded when the ping would spike almost every 30 seconds to 1000ms+. Consequently resulting in a mass fail by me getting owned by some cheeky fifteen year old sod.

A week later when I found some time to investigate I started reading all sorts of issues regarding the power requirements of Wireless-N USB sticks, particuarly the latest Linksys and Netgear incarnations. I unplugged all the USBs I could (except the keyboard and mouse for obvious reasons) but nothing would abate the lag spikes. A simple ping test revealed it was taking 40ms to ping my router on the other side of the bloody room (less than six metres). A quick check with my Dell laptop revealed all was fine with Virgin’s supplied D-Link DIR-615 router as it was returning the proper low 1ms pings.

Biting the bullet I got myself a Linksys WMP600N internal Wireless-N PCI card . This works like a charm and lag spikes and high pings are history. My ping is now averaging around 15-18ms! The other obvious solution would be a powered USB hub but I cannot say whether or not this would have been successful.

Great package Virgin Media (when you eventually get it), but don’t bother with crappy USB wireless sticks if they are just going to poison what should be an other worldly experience.

PowerPivot CTP expired

Before I go on a rant I would like to point out what a joy PowerPivot is. I found it a marvellous feature of upcoming Microsoft products and the implications for the Business Intelligence community are huge. I had been using it consistently for a couple of months in my spare time, exploring its potential for my present employer after seeing it at the SQLBits V conference back in November. I had nothing but praise.

This morning I got back from my Easter break ready to do a demo to the company only to be greeted with an expiry message. This would not have annoyed me so much if:

  1. I had been informed PowerPivot will expire during the original installation.
  2. A warning was given that an expiry was looming.
  3. PowerPivot was not free.
  4. A replacement download or installation was available to keep it rolling.

On the MSDN forums the only solution appears to be disable the Windows Time service and then set your clock back before April 1st. This can cause issues in other applications though, so not viable if you are trying to perform a smooth demonstration.

Microsoft please remember to take your crazy pills. Seems a little insane to expire a FREE product, particuarly before the official release is available.

To host or not to host?

I have now got to a point where I need much more control over my blog as I want to start posting detailed code. Time to get the self hosted version. I will be following the WordPress guide. Wish me luck and see you soon at devmau5.com/blog.

The GETDATE() of MDX

I have been repeatedly asked to produce queries in MDX across a number of SSAS cubes that use the query execution date/time and I have reused this little gem repeatedly:

StrToMember("[Date].[Date].[" + Format(now(), "yyyyMMdd" + "]")


Here it is in a complete query that returns a measure over the last 4 days from the time of execution:

SELECT
    [Measures].[Menace]
ON COLUMNS,
    LastPeriods(4,
        StrToMember("[Date].[Date].[" + Format(now(), "dd-MM-yyyy") + "]")
    )
ON ROWS
FROM [CubeOfDoom]


MDX has no GETDATE() function to obtain the member associated with the current date/time as it is up to each implementation how the date/time dimension is populated and how its members are named. If you populate your date/time dimension up to the time period of execution you can make use of DefaultMember or CurrentMember but what if the way the dimension is populated has to change in the future or the dimension is populated ahead of time?

This method may not be the most efficient but it is the most robust I have come across to date. If anyone has a better way drop me a comment. Always looking to improve/evolve.

Region lock begone

When at home I like to have some of my favourite shows in the background while I type away. Some of my favourites animated shows include Aqua Teen Hunger Force, Sealab 2021 and Robot Chicken off Cartoon Network’s Adult Swim. Living in the UK means my DVD players are typically locked on Region 2 with a max of 5 changes – thanks Sony. Almost all these shows are only available as Region 1 discs – you see my dilemna? I have unlocked the region protection on previous drives I have owned and have now hunted down a solution for my Dell desktop and laptop.

If you have a Lite-on, which is sometimes disguised as a PBDS drive, download the simple program located here and run the executable. The little app resets all counts at the click of a couple of buttons. Fantastic. Now you can have all the Stimutacs all you want. BE WARNED though you still have the OS counter and any software player counters to contend with as outlined here.

If you want to get your hands on some Aqua Teen or Sealab discs in the UK you can find them at www.playusa.com

Silverlight and fonts

In this post I outline how I got a custom font to work in Silverlight after having some issues with compatibility.

While the winter here in the UK seems to have no end in sight this year I have been trying some new things out with Silverlight 3. I got quite bored of using the standard fonts and decided to start looking around for some slick fonts to use in my projects. Using the awesome Identifont I came across AUdimat by Jack Usine.

To get your font into your Silverlight project all you have to do is add the file and make sure the project has marked it as a resource. Then, to apply the font in your page just set the FontFamily attribute like so: FontFamily="filename#fontname". In my case this was: FontFamily="AUdimat-Regular.otf#AUdimat". Too easy? You bet ya.

Ran my project and got a load of garbage output from my test TextBlock “This is some text”. Doing some digging I started to discover the world of fonts and Silverlight is not one of rainbows and fluffy bunnies. This article from Silverlight Spy is the most comprehensive at outlining font support with Silverlight and provides a brief history of fonts.

The outline format of the AUdimat font was causing the problem so I had two options; find another font, convert it to a compatible format. Given the fact I had spent a while searching for the exact font I wanted I went with the second option. To accomplish this I was lucky to find the FREE Online Font Converter. Now I had my font in TrueType format I added it back to my project, changed my FontFamily attributes to FontFamily="AUdimat-Regular.ttf#AUdimat" et voila. A beautiful rare font in my Silverlight app. A big thank you to those sites I made use of.

Return top