Archive for November, 2009

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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags:

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:

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: , ,

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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags:

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. ;)

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: , ,

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)?

  • Twitter
  • Facebook
  • Share/Bookmark

Tags:

FBCon 09 – “live” feed

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

  • It’s  8:25, conference rooms are getting the last polish. You can also deduct, that I successfully found the place.
  • The conference is held in Bauzentrum in München. There’s a lot of stuff for people who are building houses etc. in hallways. I like it. :)
  • Session from Dmitry. Good for people asking about limitations and features, why are/aren’t there. The final statement is that Firebird Project isn’t trying to implement all features from Oracle, MS SQL Server, PostgreSQL, … ;) But project is definitely open to good ideas from other projects. And no experiments with features.
  • The trace and audit session from Vlad just finished. I like this new feature, but I was not paying attention to during development, so this was a nice summary of my shattered knowledge. But what I don’t like is the configuration file format – mix of XML and standard text config file. :( And same for output, it’s not some kind of “computer ready” format, like i.e. again XML, it’s just a text, which will be for people harder to parse – but Thomas Steinmaurer already created one application. However the API is open, so probably sooner or later, I hope, somebody will create plugin with this output (and maybe also one for putting the content into database – what about MS SQL and using Analysis Services? :D )
  • After these sessions it was Holger’s turn. About the replication of databases. It was interesting to see the different approach based on creating the actual query executed from inside the trigger. Sure it has some limitations and we found some also during the session, but if you’re OK with the way how it works, it’s a good and easy-to-implement solution. But I think, just because I rather wanna to have more “based on values rather than on creating commands” approach to the synchronization/replication I would probably go in a different direction.
  • After this session was my session – the PocketPC and Firebird stuff. It was, sure, the best topic of the day ;) , and because describing own presentation is weird, I would just skip it and hand you to the recording which will be available, for sure.
  • The final session was a all-hands-session about the Firebird and performance. There’s nothing special to write about it, because everybody put his/her input it was better to be there than not.
  • And maybe the most important part was the city sightseeing and dinner during the evening. These “after-events” are great opprotunity to ask anybody you choose about your problem or give an interesting feedback, hence if you’re not here, you’re definitely missing something. :)

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

  • The first session today was from Vlad about mainly some MS C++ runtime issues with recent changes in WinXP, Vista and Win7. If you were deploying the server in the meantime (or you’re using older (sub)versions) it was definitely worth listening. The current versions of Firebird server have not these issues, because it was successfully solved.
  • Following session was from Alex about new protocol features, mainly in 2.5. As a provider writer I was a nice overview of what I will need to do in next couple of months to catch up with server. :) Also if you wanna to use some brand new features, this session was a good opportunity to see what’s going on inside and what you can do (i.e. if you components are not supporting all options).
  • If you’re reading this, you missed my presentation, which was the next in schedule. Again I’ll skip any description.
  • Session finished right now was Holger’s about backup and restore. Because I saw last presentation from Ivan Prenosil last year about (I think) 10 ways of doing backup I was not expecting something new. But in fact there was something new. The IBExpert except the IBExpert itself has a really nice set of tools, like a scheduler for backup & restore which you can very easily plug into your environment and use it for almost hot backup of your database on different server. Similarly for dealing with metadata changes and pumping data out and in.
  • The final session from regular speaker was Dmitry’s about the lock manager. To be honest I was never paying to much attention to fb_lock_print utility and I had only a very limited knowledge about it. But this presentation, especially demos, widened my knowledge little bit and I know, at least, what useful information I can find there, and if I’ll not forgot all stuff also how to use these. ;) And seeing what’s going on inside the lock manager also helps understanding the internals.
  • Really the last session of the day was continuation of talking about improving performance of Firebird (itself or tools) and sharing and exchanging the information. Because everybody was contributing with (but mainly Holger) own pieces, it was really great ending of the day.

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. :)

  • First session of the day, was classical session about “what we can expect in next development and in next year”, held by Dmitry. It was a nice overview what’s going to happen and if you’re not a member of devel and/or architect list (or you’re not reading it often) also nice summary.
  • Then the Vlad came in with new SQL features in Firebird 2.5. As I like a 2.5 a lot and I’m using it for more than a year, I was more or less familiar with these features (although I’m using only few of these). So if you’re too lazy to read release notes for betas and RCs, this session was surely good for you.
  • Before the lunch Alex had session about security. I liked the small recap of the history of security in Firebird and InterBase. Then the walk thru the new features and improvements was really good and exhausting, nice samples. I’m really looking forward to Firebird 3.0 to test all new security, mainly authentication functions. This could also make life easier for i.e. hosting providers offering Firebird.
  • Once again (and for last this year) skipping my session. (BTW I realized that even if you’re reading this text, there’s change, that you’ve seen my presentation, so my yesterdays statement wasn’t completely correct.)
  • The session about the BI tools and Firebird from Roman was really nice. My focus is mainly in OLTP, and I’m looking to OLAP world only little bit, and this was a really nice for me to see also the other side of some problems and see, for me unknown, tools and what is and what is not possible and how hard or easy (desining some transformation looked really easy and quick, at a first sigh).
  • Following session was from Roman too. Simply about the Java driver and using it. It was really nice to see how it works in Java world and what problems they’re facing and comparing these in my head with .NET world problems. The example of Hibernate was nice too, as it’s a similar tool to Entity Framework and “father” of nHibernate, so I was able to briefly look to different approach for O/M mappers solution.
  • The final (of the day) + final (of the conference) session was simply roundtable, where every developer from different parts of Firebird project (core, .NET, Java, …) introduced little bit the subproject he is working on as well as him/herself. I was expecting people asking little bit too much about these subprojects and maybe sharing ideas and needs, but the discussion didn’t furiously started. A pity.

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).

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: ,

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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: , , , , , , ,

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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: ,

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?

  • Twitter
  • Facebook
  • Share/Bookmark

Tags:

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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: , ,

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. :)

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: ,

Š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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: ,

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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags:

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.

  • Twitter
  • Facebook
  • Share/Bookmark

Tags: , , , , , , ,