Showing posts with label SQL Server CE. Show all posts
Showing posts with label SQL Server CE. Show all posts

Tuesday, June 16, 2009

Entity Framework & SQL Server CE problem

The SQL Server Compact Edition throws a very unhelpful SqlCeException exception with the message:

"A parameter is missing. [ Parameter ordinal = 1 ]"

when using the entity framwork SaveChanges() method if you set a decimal property to a value with more decimal places than the database column has been configured to store.

For example, if you have a database table "header" with a Numeric 18/3 column "Height" and execute the following code, the "A parameter is missing" will be thrown.

  EntityFrameworkSQLCEEntities entities = new
EntityFrameworkSQLCEEntities();
            
  Header header = Header.CreateHeader(Guid.NewGuid(), "Test");
  header.Height = Convert.ToDecimal(3.4555);
  //header.Height = Decimal.Round(Convert.ToDecimal(3.4555), 3);
  entities.AddToHeader(header);

  entities.SaveChanges();

Rounding the decimal to the supported number of decimal places prior to setting the property "fixes" this problem but couldn't this be better handled by the Entity Framework and/or SQL Server Compact Edition?



App.Config DataDirectory macro

By default, the |DataDirectory| macro is substituted at run-time with the folder that contains the exe.  You can override this default by changing the setting the "DataDirectory" property on the app domain as follows:

   AppDomain.CurrentDomain.SetData("DataDirectory", thePath );

Friday, June 12, 2009

Installing & using SQL Server Compact Edition data files

I've been writing a program that uses the SQL Server Compact Edition for data storage.

I configure the program (via the application config file in the set-up project) to access the SQL data file (SDF file) from the programs installation folder.  This all worked without problem on my XP & Vista PCs.

However, when installed on another Vista Laptop, my application was denied access to the SDF file and was unable to run.  This turned out to be because the laptop had UAC turned on.

This page  on stackoverflow has some solutions to this problem.  The simplest of which is to change the application to a "Full Trust" application.  You can do this in Visual Studio 2008 from the application security property page. Just check the "Enable ClickOnce Security Settings" and then select "This is a full trust application".

BTW, I've tested this on Windows 7 with UAC set on its default ("Notify me when programs try to make changes to my computer").