Saturday, March 28, 2009

Earth Hour – 28/03/2009

Earth Hour logo Earth Hour will be today at 8:30pm local time. Non-essential lights and electrical appliances will be turned off for one hour. I’m not sure it is very useful, but at least it turns attention to climate change.

Some websites change their design from colorful to black (like Blackle). Some of the advertise themselves as energy savers. Well, I’m not sure about it. It used to be useful in the age of cathode ray tube (CRT) displays. Black color used much less energy compared to white color. Unfortunately liquid crystal displays (LCD) employ a totally different  technology: they covers light to display dark colors. It means it doesn't matter the color you are displaying, the energy cost will be almost the same (or even more).

So use black background if you are using CRT displays – and use whatever color you favor if you are using LCD display.

And take part in Earth Hour if you wish. Probably I will. Turn off what you can for an hour at 8:30pm local time.

Thursday, March 19, 2009

Entity Framework – Set reference from method

Usually entity types are returned from processing methods in a layered architecture. E.g. I have a Product and I want to set the currency of the price which is stored in a different table. It means I have a foreign key to the price.

I tried to set the reference in Entity Framework something like this:

  1: Product product = new Product(); 
  2: product.Name = "aaa"; 
  3: product.Price = 12; 
  4: product.Currency = CurrencyManager.GetCurrency("USD"); 
  5: context.AddToProduct(product); 
  6: context.SaveChanges();

Unfortunately this won’t work. If GetCurrency() returns a currency returned by a LINQ query, it will be referenced by a context different from my current context. An object can’t be attached to two contexts at the same time.

The solution is simple. In the GetCurrency() method you will need detach the object returned by the query:

  1: var currencies = context.CurrencyCode.Where(c => c.Code == code);
  2: if (currencies.Count() > 0)
  3: {   
  4:     var currency = currencies.First();
  5:     context.Detach(currency);
  6:     return currency;
  7: }
  8: else
  9: {
 10:     return null;
 11: }

And when you call the GetCurrency() method, simply attach the object to the current context and set the reference to that:

  1: var currecy = CurrencyManager.GetCurrency("USD");
  2: context.Attach(currency);
  3: product.Currency = currency;

Monday, March 16, 2009

UML Object Constraint Language

I’m using Object Constraint Language (OCL) for a small MDA project. OCL is a constraint language for UML and part of the standard. I have a small experience in F# and OCL reminds me to functional languages.

Wikipedia definition for OCL:

The Object Constraint Language (OCL) is a declarative language for describing rules that apply to Unified Modeling Language (UML) models developed at IBM and now part of the UML standard. Initially, OCL was only a formal specification language extension to UML. OCL may now be used with any Meta-Object Facility (MOF) Object Management Group (OMG) meta-model, including UML. The Object Constraint Language is a precise text language that provides constraint and object query expressions on any MOF model or meta-model that cannot otherwise be expressed by diagrammatic notation. OCL is a key component of the new OMG standard recommendation for transforming models, the Queries/Views/Transformations (QVT) specification.

So OCL is a declarative language. And functional languages are subsets of these. Here is a typical OCL expression:

Team.allInstances()->forAll( t | t.name = ‘teamname’)

It reminds me a lambda expression. t | t.name in F# is something like t –> t.name.

Now I’m sure it worths learning new paradigms like functional programming. You never know what kind of language (element) you have to use in the future.

Improve SQL performance

I’m using Microsoft SQL Server 2008 Enterprise Edition on my developer machine. I used to work with SQL Server 2005 Developer Edition before.

I recognized that my machine replied unusually slowly with version 2008. The solution was setting a memory limit and processor affinity for the server. Now it is limited to 400 MB RAM and I assigned the IO and computation to different cores. It works fine and the performance has increased.

PS: If you are using Entity Framework, consider the warm-up time of the framework. Just like .NET, the first queries will be ~10 times slower and the subsequent queries will be much faster.

Friday, March 6, 2009

Visual Studio Help Blocks

I was using Visual Studio 2008 in Virtual PC when I accidentally pressed a Help button. It was a virtual machine so no one used the Document Explorer before. Visual Studio user interface became unresponsive, Help system started processing help files. I got tired of it after a few minutes.

Workaround for the problem:

  1. Kill Visual Studio 2008 (from Task Manager)
  2. Set Document Explorer thread priority to Below Normal
  3. Restart Visual Studio 2008

So help system will be initialized next time and you can continue working on your project.

Thursday, March 5, 2009

Entity Framework: Error 3007

I got an error message after updating my Entity Framework .edmx model:

Error 3007: Problem in Mapping Fragments starting at lines … : Non-Primary-Key column(s) [ColumnName] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.

Finally I managed to solve the problem. I added a foreign key to a table, so Entity Framework generated an association. The association name was the same as the foreign key field name. Designer never removes CSDL objects, so these names were the same. I had to remove the entity field to make the model valid. Now it works fine.

Importing lists to MSSQL2008

I had to import a list of country names to an SQL table using MSSQL 2008. I found a list in CVS format here:

http://tobiasconradi.com/geography/

Good, MSSQL 2008 can import from CVS. I set the language codes, delimiters, headers, even the column lengths, and… the import failed. I tried to parse CVS with different setting, but I was unable to execute the task without errors.

Finally I tried Excel 2007. I opened the CVS file with Excel, but character encoding wasn’t right. So I found the Data tab and the From text button. It’s a very convenient and fast way to import CVS. I managed to import the whole CVS file to Excel and I imported the list with the Excel file provider. Now I have beautiful list of countries in numerous languages.

You can download the country list script for SQL Server 2008. It contains two views for English and Hungarian country names.

Link: Country list (.sql)