May13

Entity Framework 4 New Operations

There are some new operations in .Net 4 Entity Framework, this a quick example of each working with VistaDB 4 and Visual Studio 2010.  These changes were mostly made to bring LINQ to Entities inline with the other LINQ providers in .Net.  Of the list below I think that Single() was the one that most people were confused about because if you used it you would get weird errors that didn’t make a lot of sense.

For a complete list of LINQ to Entities operators visit the Supported and Unsupported LINQ Methods on MSDN.

Entity Framework in .Net 4

The operations I want to demonstrate are the new Contains(), Single(), SingleOrDefault(), and DefaultIfEmpty().

SimpleTable for Entity Framework Example These operations are all new in .Net 4, and yes they work with VistaDB 4.  I started with a simple one table database called Feedback.  The only table has 3 columns:

  • int FeedbackID
  • NText FeedbackText
  • DateTime FeedbackDate

I added a few small text entries, including “I like grapes'” to search against with the Contains() operator.

More...

May06

Help on Visual Studio 2010 Help

If you have used VS 2010’s help system you may have that feeling that some changes were not as fully thought as others.  The new HTML Help 3 format may seems like a great idea, it runs as a service like an IIS website that all help instances query through.  No more hitting F1 in three copies of Visual Studio and getting three copies of the complete help loaded into RAM.

But that browser experience for the help system seems missing something to me.  Maybe the index?  Maybe the ability to actually read rather than search?  It seems optimized quite well for searching the help, but not actually following a topic.  You can’t sync the table of contents and see what other articles are in the same section of the help.

I want to cover the built in help, the Help Powertool released by Microsoft, and the H3Viewer application from The Helpware Group that allows local viewing of the html 3 help files (although Microsoft insists on calling them Help 1.0 Files).

F1 Help in VS 2010

Visual Studio 2010 Help Default Here is what I see when I hit F1 in Visual Studio 2010.  You get a system tray icon with the help service running and a browser window.

You have to already opted to install the help locally as a part of the install process, or you only talk to the remote servers at Microsoft. 

There was a bug in the beta versions that if the user selected non local help for the MSDN then ALL help was non local (even if your company put some help locally the users couldn’t see it).  I hope that has been fixed, but have not had time to test it myself again yet.

More...

April12

VistaDB 4 and Visual Studio 2010

Visual Studio 2010 - Gen EF 4Yes, Visual Studio 2010 was released today to much fanfare in Las Vegas. 

We of course had to immediately download it (along with what must be some major percentage of MSDN subscribers) and give it a test. 

Use Build 14 or higher

VistaDB 4.0 Build 14 works great with the VS 2010 Release.  Versions prior to this should probably not be used as there were some changes we had to make in order to get everything to work with VS 2010 in the first place. 

I have run through all of our NUnit tests in both .Net 2 and .Net 4 and all of them work correctly.  The Visual Studio plugins work, and the Entity Framework provider seems to be working fine as well.  The screen shot above is me generating a .Net 4 EF model on our Tickets Database.  (Note you can use the Include Foreign Key Columns only if you are using .Net 4)

More...

April02

Compact Framework is finally dead (or is it)

Click image to view originalI think many people know that we used to produce a Compact Framework (CF) provider for VistaDB.  We didn’t do it for VistaDB 4 specifically because of a couple issues that I saw coming, and couldn’t get clear answers from anyone at Microsoft.  I think since the Windows Phone 7 announcement things are finally clearing up, but Microsoft has some confusing terminology.

Compact Framework for Visual Studio 2010

A lot of people noticed this doesn’t exist.  There is no mobile story in the Visual Studio 2010 betas at all.  There were a lot of people saying it would come later, but I had suspicions over a year ago it was going away.  There is no Compact Framework for .Net 4.  But there is a new Visual Studio for Windows Phone as a part of the tools preview.

Silverlight 4 has a compact framework

But Silverlight 4 has a “compact framework” embedded in it.  Not that this is in any way shape or form similar to the actual Compact Framework.  It is a severe subset of what was already a subset of the full .Net framework.  The Silverlight subset of .Net does not include encryption, lots of XML features, ADO.Net, etc.  The Silverlight subset of .Net is really only a few core system libraries that let you write C# / VB.Net code for Silverlight.  You don’t even bind against the desktop runtime, you must target the Silverlight 4 runtime through a special project.  You cannot have a single solution with both Silverlight 4 and .Net 4 as simple output targets (you can do through linking files, etc, but not the same as you could for CF – target).

Windows Phone 7 Silverlight and compact framework

I was recently twit’d a link to a great blog by Abhinaba Basu who works on the NETCF team for Microsoft.  In his posts he discusses the Phone 7 NETCF and how it works to a great amount of detail. 

He states that Windows Phone 7 uses Compact Framework 3.7.  What follows is the summary of his post.

More...

March29

Rounding floating point numbers

A lot of confusion seems to exist around the ability to round floating point numbers, I am going to try to shed a little light on this topic.  Bear with me, the math may get a little involved, but the answers will be worth it (I think).

Float is an estimated data type

Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly.

MSDN SQL Float Article

Float is never an exact number.  The number is internally stored in more than one part and has to be processed into something we humans can understand.  That means something like 1/3 can be represented as 33%, but it is not actually a nice even number.

For the full mathematical treatment of why this is so, feel free to read the fantastic paper “What Every Computer Scientist Should Know About Floating-Point Arithmetic”.  Warning, that paper is not for the feint of heart, it has a lot of math behind it.

The important thing to pick up out of that paper is that all possible numbers between 0 and 1 cannot be represented in 32 or even 64 bits. In order to store a complicated number with decimal places it has to be estimated.  This estimation introduces Rounding Errors.  When you start with one small error, everything that comes afterwards is not what you expect, these types of errors lead to errors like the Patriot Scud Missile Error.

A C# Example of Rounding

If you run the following C# code you will see an object that has a result of 5.1400000000000006.  Even though it looks like op1 = 2.56 and op2 = 2.58.

double type example

double op1 = Math.Round(2.562, 2);
double op2 = Math.Round(2.577, 2);
double opresult = op1 + op2;

Set a breakpoint on the opresult assignment and look at the value.  Now go into your immediate window and type ? opresult.ToString() and you will see an answer that most people probably expect (5.14).  But put your mouse over the opresult object and you see the actual value.

Result of two doubles added

I know at this point it probably seems pretty confusing.  A double is what VistaDB internally stores a FLOAT column type.  Why?  Because they match pretty well in bit size and what they can do, but it does lead to an interesting rounding problem like the one above.

Lets take the same code with a decimal type, what changes?

More...

March12

Simple ways to avoid BlogEngine comment spam

Yes, comment spam is everywhere.  Spammers will never stop trying to find new ways to get their silly links into other peoples blogs.  Even though almost every blog puts the nofollow attribute on the comments, spammers still think that their site will be boosted because of the inbound links.

I recently upgraded to BlogEngine 1.6 on this blog and found that we suddenly were being attacked by 250+ spammers a day trying to get their comments on the blog.  But our existing VistaDB Blog (was running 1.5) didn’t have this problem.  What was the difference?  Was it something new in BE 1.6?  Or was it something we had done on the VistaDB blog differently?

It turns out that spammer target a few phrases they know are used by default in certain blog engines.  They already have code in their bots that know how to fill out the forms on these blogs, so they are looking for the default phrases.

Configure Filters Correctly

First thing is the make sure your Akismet is setup correctly and you have a valid key.  One non-obvious thing is that 1.6 of BE introduced a bug that allows for more comment spam to get through because if the last filter says the comment is OK the comment is allowed.

You can change this behavior by simply moving the AkismetFilter to the bottom.  Drop its priority to 2, leave the StopForumSpam at priority 1.

2010-03-10_1345

This is what my settings look like the day I reset the filters and moved their order.

This change alone cut about 60% of the spam that was getting through by simply having Akismet be the last entry.  But you can also do a few more things to through off spammers.

More...

March08

Buffer.BlockCopy not as fast as you think

I recently wrote a quick test to prove something, that ended up being wrong…

Of course this is the way of science.  You form a hypothesis, then you test.  The results should prove or disprove your theory.

The hypothesis

Ok, my theory was that Buffer.BlockCopy() would outperform our current Array.Copy routines on the page level.

When we are loading records off a page we have to extract some parts of the buffer to build up a row buffer. A typical 8Kb page may have 100 or more records packed into it, and grabbing them out can take quite a bit of time.  Each row data is a compressed byte array that may include information that points to extended row data as well. 

This is one of the differences between this same app written in C++ and C#.  In C++ you would just take a pointer to the original buffer and pass that to the row routines, you wouldn’t need a copy of the data.  But we don’t have pointers in C#, so a copy is made to get out just the part of the buffer needed by that row.

Since we want the fastest way possible to read this block of data into the row, we were looking at alternative ways to do it.

The Experiment

I wrote a program that randomly grabs a length from an 8Kb block that contains 150 records.  The length of the row was randomized (but the seed reset to the same value for both tests to ensure the same random numbers were used with both methods).  The results were surprising.

More...

March03

Visual Studio 2010 RC Update

Visual Studio 2010 RC came out a few weeks ago, and the current VistaDB 4 build 12 does not work.  Build 12 obviously came out before the RC, so it was never tested on that release.

Updating for 2010 RC

The next VistaDB 4.0 build will be updated for Visual Studio 2010 RC.  I was actually of two minds of this issue. If the final (really final) build is only a few weeks away maybe we should just wait for that build.  Microsoft has broken ADO.Net plugins, EF, and several other things we need in EVERY new build of Visual Studio 2010.

Beta 2 left a bad taste

When 2010 Beta 2 shipped we scrambled really hard to get an updated build that worked.  We literally dropped everything else to just focus on those issues for about a week.  My thinking at the time was that if we found errors and got the bugs in quickly to Microsoft they would be fixed for the final release.  We opened 10 tickets with Microsoft on Beta 2 – ALL of them were denied as “too late in cycle to be fixed”.

More...

February25

LINQPad helps you learn LINQ

Have you tried to use LINQ to query a databaseLINQPAD Logo using Visual Studio?  It can be a frustrating experience of things that compile fail at runtime, and that edit / compile / test cycle can quickly lead to hours of lost time trying to get a single complex query to work correctly.

LINQPad is an editor for LINQ?

LINQPad is sort of like a Notepad, you can write and edit .linq files using it.  But that is where the Notepad similarity stops. You can execute your LINQ queries and see the results without having to run your application.  It is truly something you have to watch in order to believe how much more productive it can make your writing of LINQ.

How I found LINQPad

When I was first working on the VistaDB product store and account manager writing LINQ queries against my Entity Framework objects was incredibly frustrating.  Most of the documentation and samples I found was for Linq2Sql, which is similar syntax… But not the same. And worse, most of the syntax compiles fine, but blows up at runtime with cryptic error messages.

My typical dev and test cycle was around 5 minutes to compile the DAL / Site, login to the account, navigate to the correct page, and visit the yellow screen of death from asp.net.  It was not fun, so I started working in a stand alone tester app I built just for this purpose.  Write the query, compile, debug, step, step, read exception and try to decipher it.

I was reading a post on Stack Overflow about one of those cryptic errors when someone suggested to the poster they use LINQPad to test their queries first before putting them into their apps.  Wow, what a great idea!  Where was this tool?  (You can download it for free from Linqpad.net )

LINQPad to the rescue

LINQPad allows you to execute single LINQ commands against an existing EF model, or even to write dot net code in the editor and execute it like a little dynamic dot net environment.  The main application is free, but there is an auto-complete feature to the editor that you must pay in order to activate. Believe me it is worth it to pay for the license, you also support the author and show him the application is worth money.  The license is very inexpensive and well worth the price in order to get intellisense like behavior on your LINQ queries.

More...

February09

10 things to make your desktop database apps better

NOTE: This article was originally published on the VistaDB blog, but has been moved to this location permanently.

Each of these items could be a blog post unto themselves, but I am going to try really hard to not be too verbose and just cover the core of the concept and why you need to do it.

Data driven applications rely on their databases

Everyone knows that any app driven by data is much more than just the app.  In most cases the app without a database doesn’t even function, or fails to function properly.  If a database is an integral part of your application, then shouldn’t you be doing all you can to ensure it stays healthy and prepare for the worst case events of corruption or dead drives?

This week we have been contacted by 5 long time users who suddenly lost or corrupted their data.  Two of these were end user data that is going to cost a huge amount of labor to reproduce.  In all of these cases the following simple procedures would have prevented the situation entirely.

More...