Archives for November, 2009

30
Nov

Mysterious problem when fetching data

Sometimes you’re talking about something on trainings and conferences and … and you simply forgot about it when you use it – it’s like being able to think in A > B way but not B > A.

This time it’s about views in Entity Framework. If you add a view into your model, it’s likely it will have a wrong key set up. Or maybe it’ll be missing. And because if two records have same EntityKey, they are considered same, also skipping the very expensive materialization. I added one view into my model and didn’t check the key. It was working fine, until last week, when the application started behave “differently”. I checked the database and view was providing proper data. Fetching was just reading from this view, no other processing. After some serious debugging and getting wrong data, in my case all records had same values as the first one. When I switched the projection it was good. I started to think about blaming EF. And I was wrong, sorry guys in EF team. The reason was, as you might guess, wrong key setted up. My view has a key composed from three columns, but only one was used. This caused the EF during the materialization to skip other rows, because the first column was same, thus getting wrong results. Fixing the key solved the problem immediately. It’s more embarrassing because if you look into the XML file, there’s comment telling you to check the key, because the inferred one can be wrong.

Anyway, now I know also the B > A way, which is even better as it was my personal experience.

29
Nov

Photos from FBCon 09 München

Starter for (but not only) morning:

Holger Klemt:

Stefan Heymann doing the same as me:

Dmitry Yemanov:

Vlad Khorsun:

Roman Rokytskyy:

In between sessions:

During session:

In between sessions:

Everybody is solving problems during conference:

Talking about (not only) Firebird in hallways:

25
Nov

New firmware (2.3) for Kindle

This morning I looked to my RSSs and found post from Scott Hanselman about new firmware for Kindle. Although Scott was applying it manually, I got it through network immediately. After about 3 minutes Kindle rebooted and was ready to work.

You can read about the new features on Amazon page, I was especially looking forward to build-in PDF reader. Right now I tested only two or three files, all displayed correctly. But you can’t change font size, so if the font is too small in PDF, you will read nothing. With new screen rotation it’s little bit better, but the page will not fit display completely, of course, so you’ll be hitting next page button more. ;)

The battery life I can judge now. But anything that improves the batter life, even 1%, is good.

24
Nov

Comparing date only in EF

From time to time you may need to compare only date part of datetime/timestamp field in your database i.e. for filtering. In EFv1 this is a little bit problem, in EFv4 (now beta 2) better.

Let’s start with EFv1. If you try to write something like this:

.Where(x => x.DateTimeColumn.Date == DateTime.Today)

You’ll end up with nice exception. Simply EF is not able to translate it. To solve this problem I created handy (for me) method, that will do simply expansion comparing Day, Month, Year, so you don’t have to write it over and over again.

public static Expression<Func<TElement, bool>> DateEqual<TElement>(Expression<Func<TElement, DateTime>> valueSelector, DateTime dt)
{
	if (valueSelector == null)
		throw new ArgumentException("valueSelector");

	ParameterExpression p = valueSelector.Parameters.Single();

	Expression ex = Expression.And(
		Expression.Equal(
			Expression.MakeMemberAccess(valueSelector.Body, typeof(DateTime).GetMember("Day").Single()),
			Expression.Constant(dt.Day)
			),
		Expression.And(
			Expression.Equal(
				Expression.MakeMemberAccess(valueSelector.Body, typeof(DateTime).GetMember("Month").Single()),
				Expression.Constant(dt.Month)
				),
			Expression.Equal(
				Expression.MakeMemberAccess(valueSelector.Body, typeof(DateTime).GetMember("Year").Single()),
				Expression.Constant(dt.Year)
				)
			)
		);
	return Expression.Lambda<Func<TElement, bool>>(ex, p);
}

So you can write i.e.:

.Where(Ext.DateEqual<DateTimeEntity>(x => x.DateTimeColumn, DateTime.Today.AddDays(-20)))

One modification I have in my head, is to extend it to support comparing two columns in database. But that shouldn’t be hard.

On the other hand, EFv4 contains couple of new canonical functions, for datetime/timestamp as well. One that’s useful for this case is TruncateTime, also exported for LINQ usage with EdmFunctionAttribute. With it, you can write queries little bit easier. Still not directly .Date, but i.e.:

.Where(x => EntityFunctions.TruncateTime(x.DateTimeColumn) == DateTime.Today)

Comparing two columns is available too. Sure, your provider needs to support this new function, but that should be no problem for all provider writers.

Maybe the future EF improvement could be to support .Date for translation too. ;)

22
Nov

IsLoaded and EntityKey

Today I discovered behavior of Entity Framework that is completely logic, but may surprise you, as it surprised me. Simply consider two entities with 1-N relationship. If you’re loading the “N-entity”, you know, whether the “1-part” is in database, because the foreign key column has some value or is null. If it’s null and you’ll check the IsLoaded property of EntityReference<T> it will say true, even you didn’t call Load method. And it’s correct and smart, because you know that the value will be null (because of the null foreign key column).

But if you set the EntityKey of EntityReference<T> (because you’re kind of simulating (i.e. for performance reasons) creating the association thru FK in EFv1) and SaveChanges, even after this, the IsLoaded will be true, Value will be null, but it will not be correct from point of view in current time. Sure working with EntityKey directly isn’t the recommended way, unless you know what you’re doing. On the other hand, if the EF is smart enough to state, that the EntityReference<T> was loaded for this null case (even if not explicitly), maybe it should be smart to invalidate this state when something changes in EntityKey (and changes are saved into database).

What do you think? Is it true or should you expect and know this when playing with EntityKey (as you should be probably enough experienced when using EntityKey)?

19
Nov

FBCon 09 – “live” feed

The conference is about to start. I’ll write here more and more from it, read it top to bottom.

Day 2 is ready. Everybody fresh and relaxed ;) . Weather in München is nice.

And here we’re. The last day of conference. Everybody is looking little bit exhausted, but I’m sure we all are gonna to survive no matter what. :)

What to say about this years conference? It was good definitely. I was really enjoying my presence as well as my presentations. So see you next year, maybe face to face maybe reading this feed (if I’ll be doing it).

16
Nov

Entity Framework 4 – MS Fest 2009

Nová verze Entity Frameworku už pomalu klepe na dveře a proto není od věci podívat se, co nového nabídne. V rámci přednášky na MS Festu projdu největší změny a vylepšení. Rozhodně je na co se těšit – pokud již EF používáte, tak na ulehčení života a pokud ještě ne, tak na ještě větší lákadla jej použít. A vzhledem k plánované večerní akci bude prostor i pro kuloární diskuze, nejen o EF.

10
Nov

Foreign keys in conceptual model in Entity Data Model

When I first heard about exposing foreign keys in conceptual model I was getting crazy. It’s the object world, you shouldn’t think this way, in a relational world. On the other hand, working purely in objects can be a pain, especially in web development, and performance problem. Sure, foreign keys can solve you some “problems” (and maybe add some) without tweaking the EntityKey or doing stub entities. Which is hard when dealing with POCO.

But is this a really way to go? Exchange cleanness for comfort.

I don’t know. I was doing the EntityKey magic, and it was magic, because it was not clean. Now I can do it easily, but. For POCO it’s harder, as there’s no EntityKey, but again, you have plain objects – why to deal with foreign keys? What if you move to some non-relational store?

But still one piece is left. The M:N association. Here I cannot use any foreign key, so it’s back to hacking or pure objects. And maybe this is the point. If creating some kind of comfort here for developers, why not to find some way for complete story? Wouldn’t it be nice?

Now I know (see how good is trying to write your own ideas). I don’t like the current concept of FKs. It’s too easy, and just too replicating behavior of relational world. But if there would have been some improvement for the other types of associations to cover all types in uniform way, I would vote for it with both my hands. Although still saying that it’s not nice but acceptable, as relational databases – it seems – will be with us for sure next couple of years.

8
Nov

Implicit lazy loading on by default in EF4

The last beta (Beta 2) of Visual Studio 2010 with EF4 contains one “interesting” change. The implicit lazy loading is turned on by default. And I’m not sure I like it.

I can handle the fact, that there was a strong demand for it (as Julie pointed) and that for LINQ to SQL people or beginners this may be much easier. But from a database guy perspective this is hidden evil, especially if you’re not the only one developer in project. You don’t know when you’re hitting the database and maybe worse, everything seems to be working fine (all data are there), but it’s a performance problem when you deploy the application from local/test environment. And how surprising will it be, when the context will be gone.

Maybe I change my opinion after trying to work with it. But now, I’ll rather use eager loading or I’ll be explicit with my calls to database. To be on safe side. What do you think?

5
Nov

VMWare Workstation 7 and Visual Studio 2010 rendering problem

I recently migrated from VPC to VMWare Workstation. So far all looks good only one problem I faced two days ago. When I run Visual Studio 2010 (Beta 2), the menu rendering was odd, text in code editor was disappearing and so on. After some searching I found, that it may be caused by 3D graphics acceleration turned on in VMWare Workstation. Surprisingly you have to turn it off – either in system, setting DisableHWAcceleration in registry (it’s for WPF) or unchecking the 3D acceleration in VM’s settings. After this adjustment (my choice was the VM level, as it may be improved later and it’s IMO easier to turn it on), all works great. I hope when the RTM of Visual Studio 2010 will be released, this will work as expected.

5
Nov

Virtual PC ==> VMWare Workstation

I’ve been using Virtual PC (2007) for a quite while and very heavily. In fact my entire development environment is in VMs. But I was getting more and more upset with Virtual PC. The version 2007 SP1 is/was kind of old, missing some new features and improvements. When testing the new Windows Virtual PC I felt like it’s focused more on standard users and mainly to run some XP applications in Windows 7, not as a full size virtualization tool for advanced users (but I may be wrong, it’s just feeling).

So I tested the VMWare Workstation 7, couple of days ago. Shortly, I’m sold. It’s much more mature (again my feeling), has really advanced features for geeks as me (like the snapshots – I created my own way with Virtual PC, but in VMWare Workstation it’s much easier) and the Unity – I like it (I know Windows Virtual PC has this too, but VMWare Workstation seems to support more systems).

The migration was the only challenge. All my VMs in Virtual PC contained software and setups that I didn’t want to reinstall from scratch. I’m running some XP boxes, where the migration was OK. I was just forced to do the activation again, but no problem. The Windows 7 (this instance was RC) it ended in BSOD. System suggested me some repair utility during next boot, but it didn’t help. So the W7 system will be probably reinstalled, which isn’t so bad, because I was planning to install the RTM, just little depressing. The Linux boxes, based mainly on some Live CDs, ran without problems. So the overall result is good, I think.

From the performance perspective, I can’t judge the performance of VM itself. I was more or less happy with Virtual PC and I don’t have some exact numbers to provide. But what’s faster is saving and restoring the state of the machine and I’m using this a lot.

I hope I’ll not find some critical problem that will make me hate the VMWare. :)

5
Nov

Školení Firebird (2.)

Společně s Databázovým světem tu máme další Firebird školení. Více informací, včetně možnosti přihlášení na: http://www.dbsvet.cz/view.php?cisloclanku=2009110403.

2
Nov

Kindle (International) – další (2.) zkušenosti

Asi před týdnem jsem popisoval první zkušenosti s Kindlem. To jsem ho měl v ruce sotva den, takže všechny neduhy a neergonomické vlastnosti musely přijít logicky později.

Dobrá zpráva je, že mě Kindle pořád baví a stále myslím, že je to skvělé zařízení (a služba). Rozhodně nelituji. Doufám, že mezinárodní nabídka se bude jen rozrůstat.

Ale teď k těm horším stránkám:
Tloušťka: Kindle je tenký a to je naprd. Když ležíte nebo i sedíte, nejde pořádně čapnout jednou rukou, je to nepohodlné. Knížka je přeci jen tlustší a lépe se drží. Nicméně možná to vyřeší nějaký obal (kterému ale zatím odolávám).

Displej resp. okraje: Toto souvisí s předchozím. Knížku chytnete kdekoli za stránku a když jste v místě, prostě prst přesunete. Jenže na Kindlu by byl displej za chvíli totálně upatlanej. Ještě že jsou kolem displeje rozumné okraje.

Nabíjení: Na můj vkus trvá nabíjení dlouho, asi tak tři hodiny. Docela nepříjemné, když si chcete číst a jste omezeni kabelem.

Naopak pozitiva, která jsem “objevil”:
Čtu každou chvíli a čtu více věcí najednou, podle chuti – zařízení si pamatuje, kde jsem skončil a e-ink čte se opravdu pohodlně. I se zapnutým GSM modulem, každodenním stažením novin a sporadickým mrknutím do Kindle Storu, vydrží Kindle čtyři dny (pokud jsem síť vypnul, úbytek baterie se zpomalil odhadem 3×).

Doručování novin je nádhera, škoda, že nelze (zatím) najít nic co by více pokrývalo lokální dění. Ale i tak lze třeba v New York Times najít dobré články.

Nákup knih je trochu omezen. Některé tituly nejsou pro Evropu (kde mám Kindle registrován) dostupné. Takže, buď se s tím smíříte, nebo změníte adresu a budete “podvádět”. Jinak jakmile máte aktivní 1-Click payment, nakupujete bez problému a knížka je opravdu do minuty stažena. Díky ukázkám z knih se dá docela dobře rozhodnout před koupí. Nicméně zatím všechny ukázky co jsem zkusil, obsahovaly několik počátečních stránek, což u jedné knížky znamenalo vidět jen copyright-bla-bla-bla na začátku a pak obsah.

Jak jsem již psal, ze čtení displeje nebolí oči, ale chce si to mentálně na princip jeho fungování zvyknout. Protože fakticky nesvítí, nemá smysl Kindle vytahovat ve tmě. ;) A stejně tak protože je to elektronické zařízení, ne papír/kniha, mrholení atp. mu nesvědčí.

Konverzi dokumentů jsem zkoušel primárně z PDFka. Jednak už vytvořené jinými nebo moje, do kterého jsem vložil např. text z webové stránky. Převod funguje dobře, dokonce i dvousloupcová sazba je z 99% pořádku. Nepozorností jsem si nevšiml, že z DOCu není převod označen jako experimentální (pouze PDF a DOCX) a tedy pokud chcete něco do Kindlu dostat, je užitečnější poslat to v DOCu, je-li to možné, výsledek je lepší než z PDF. Zatím veškerá konverze rychlá, do minuty.

Systém placení bych řekl je nastaven rozumně, aspoň co jsem mohl vyzkoušet. Platíte jen při nákupu knihy její cenu, event. předplatné novin. A $0.99 za 1MB při doručení konvertovaného dokumentu bezdrátově, megabajty se zaokrouhlují nahoru a platby pod $5 se agregují.

Pokud máte dotaz, opět jsou komentáře k dispozici.

2
Nov

FBCon 2009 München – .NET + Firebird will be there

Next Firebird conference is coming. This year in München, Germany. I’ve never been in München though it’s pretty close to Czech Rep. thus I’m looking forward.

You can find info about conference at firebird-conference.com. I’ll be speaking there too, hence if you’re interested in Firebird and .NET you should definitely come. Every day I have one session. I’ll be covering new (2.5+) protocol implementation in .NET client for Firebird 2.1, Entity Framework support (also new in 2.5) and finally you’ll see how to create Windows phone (formely PocketPC, …) application and accessing Firebird database.

And the conference will be also great place to meet people you know from list etc. and talk face to face about your Firebird related problems, challenges and solutions. If you have any specific need to show (related to my three sessions) feel free to drop line in comments.