Monthly Archives: March 2010

Entity Framework Designer file (EDMX) opened in both designer and XML Editor

I’m using a lot of features from Entity Framework. In fact I think sometimes I’m abusing some features or pushing these to limits. Due to this, I’m often using designer as well as directly editing the XML (maybe if I could do “Update Model from Database” I would use XML almost all the time). But it’s a pain to be forced to close the designer when opening the file in XML Editor and vice versa.

Luckily there’s a solution. Open another instance of Visual Studio, there open the file in one “view” and the other “view” open in your solution as normal. When you change your file in one instance, the Visual Studio on the other will detect it and you can reload the file.

This will save me (and hopefully you) couple of unneeded clicks.

Running queries in parallel with Entity Framework (and not only with it)

From time to time I have to run two or more queries that I know will always be two or more – like some first/skip records and also total count. If you write it as two queries and execute, that means two round trips to database. Although it may not matter if the network latency is very small, why not to challenge myself and try to find some workarounds.

Sure you can create some stored procedures and get the data back from these, but I was thinking about more LINQ to Entitiesish way. I recalled a way I one time used inside one project. Although it was done in pure SQL, it, as it turned out, works, kind of, for LINQ to Entities as well.

The idea is using “one row table” and put the queries as columns. Let me demonstrate:

select
  (select foo, bar from table1 where ...),
  (select baz, foo from table2 where ...)
from OneRowTable;

Where the OneRowTable can be specially created table or i.e. for Firebird RDB$DATABASE or for Oracle Database dual. It isn’t the nicest SQL (and also challenges optimizer), but works. In columns as queries you can put anything you want as long as it is syntactically correct.

OK, what about the Entity Framework or LINQ to Entities respectively. I created the “one row table” first:

create table OneRowTable(x bit primary key);
insert into OneRowTable values (0);

The table needs to have the primary key to be able to import it into entity model, the datatype doesn’t matter (I was using MS SQL, hence the bit).

What about the queries? Similar approach:

var allinone = context.OneRowTable.Select(_ => new
{
	AData = context.a.Where(a => a.x.HasValue && a.x.Value > 10).Select(a => new { A1 = a.id, A2 = a.id * 2 }),
	BData = context.b.Where(b => b.id < 999).Select(b => new { B1 = b.id, B2 = b.y }),
});
string query = (allinone as ObjectQuery).ToTraceString();
var data = allinone.First();
var adata = data.AData;
var bdata = data.BData;

The a and b are my testing tables. You can check there’s only one query executed. Encapsulating this into some method is only piece of cake.

And how the query looks like? Well for my MS SQL test:

SELECT 
[UnionAll1].[x] AS [C1], 
[UnionAll1].[C2] AS [C2], 
[UnionAll1].[C1] AS [C3], 
[UnionAll1].[id] AS [C4], 
[UnionAll1].[id1] AS [C5], 
[UnionAll1].[C3] AS [C6], 
[UnionAll1].[C4] AS [C7], 
[UnionAll1].[C5] AS [C8], 
[UnionAll1].[C6] AS [C9]
FROM  (SELECT 
	[Project1].[C2] AS [C1], 
	[Extent1].[x] AS [x], 
	1 AS [C2], 
	[Project1].[id] AS [id], 
	[Project1].[id] AS [id1], 
	[Project1].[C1] AS [C3], 
	CAST(NULL AS int) AS [C4], 
	CAST(NULL AS int) AS [C5], 
	CAST(NULL AS varchar(1)) AS [C6]
	FROM  [dbo].[OneRowTable] AS [Extent1]
	LEFT OUTER JOIN  (SELECT 
		[Extent2].[id] AS [id], 
		[Extent2].[id] * 2 AS [C1], 
		1 AS [C2]
		FROM [dbo].[a] AS [Extent2]
		WHERE ([Extent2].[x] IS NOT NULL) AND ([Extent2].[x] > 10) ) AS [Project1] ON 1 = 1
UNION ALL
	SELECT 
	2 AS [C1], 
	[Extent3].[x] AS [x], 
	[Extent4].[id] AS [id], 
	CAST(NULL AS int) AS [C2], 
	CAST(NULL AS int) AS [C3], 
	CAST(NULL AS int) AS [C4], 
	[Extent4].[id] AS [id1], 
	[Extent4].[id] AS [id2], 
	[Extent4].[y] AS [y]
	FROM  [dbo].[OneRowTable] AS [Extent3]
	CROSS JOIN [dbo].[b] AS [Extent4]
	WHERE [Extent4].[id] < 999) AS [UnionAll1]
ORDER BY [UnionAll1].[x] ASC, [UnionAll1].[C1] ASC

Not exactly the original shape. The translator took another way creating two one row results and using union all to get it into one query. Except this, the query is in general the same (the explicit joins are as result same as the subselects, though little bit more confusing in this case).

Again, this isn’t general purpose way of doing it and may result in worse performance than running queries separately and I would recommend using it only after careful testing and on controlled limited set of queries.

Missing notes for new IEnumerable<string> methods

I noticed is that the documentation for some of new IEnumerable<string> methods in System.IO I wrote before is missing important parts. For example, the old method has notes about unstable sorting and about the different behavior for patterns where the extension is exactly three characters long. But the new one is missing these notes and these are quite important, in my opinion. I hope this is just because we’re in RC stage and in RTM all will be ready. Else it could cause confusion when using new methods (until you find the string[] version (or this post ;) )).

IEnumerable<string> result for some methods in System.IO

The .NET Framework 4 includes some nice new methods in System.IO namespace. There’s a lot of methods on Directory class etc. returning string[]. That’s OK until you try to enumerate directory with thousands of files (for example). Then you’re waiting for complete result even is you just need couple of first item. The new EnumerateXxx methods are solving this as these are returning IEnumerable<string> (i.e. EnumerateFiles).

I’m looking forward to use these in ID3 renamer when .NET Framework 4 will be released.

Second feelings about the new SSD

Let’s call it a day. I’m now running my new Intel X25-M SSD for a little bit more than a day, but more importantly one working day. Shortly, I’m pleased.

I don’t know, maybe my expectations yesterday were too high or maybe I wasn’t just observing (or had no time) the useful stuff. And as correctly this morning my friend Petr Kaleta pointed, I’m not only comparing the disk itself, but also the rest of the system (Dell Latitude D620), whether it’s able to keep up with the disk.

Anyway new observation from today is seeking. Simply forgot about it. First you not hear it – no moving heads, no sound. So somewhere in background you feel something is different. And other fact is that the “seeking” is so fast, you’ll waste other time checking email etc. My Opera, right now with 30 tabs, starts with same speed as empty Chromium was doing on old disk. And it’s not only about the start, when you see the application it’s ready to work with. The post-start processes are already done. Same with Outlook. It behaves really instantly when switching folders, searching, checking calendar etc.

Other piece I noticed is working with virtual machines. You know, I run all my development environment in VMs, hence I stress it a lot. The start up is roughly two times faster as I wrote yesterday. The suspend is even faster. But when you suspend one and start another it’s significant. Normally I was waiting a long time, the disk was overloaded. Now, it doesn’t matter. Same speed.

Another chapter is working inside the VM. The applications there are working faster, thanks to the good seeking behavior. The Visual Studio 2010 RC there starts in under 10 seconds (loading solution with one DAL and BL library and one console application). The first compilation is about four times faster, subsequent ones are not such a big improvement as good piece of work is CPU bound. I like this improvement a lot.

The battery life seems to be about 50% better. It depends very on what you’re doing (and in what shape your battery is), and I even don’t remember exactly how quickly was my battery discharging before. So it’s based on my estimate how many tasks I was able to solve before need to recharge. :) Bear with me.

Last stuff I was carefully observing was installation of Opera 10.50, which came out today. After 15-20 seconds the installation was done. The progress bar flew couple times from left to right. I put this down again to great seeking times (the installation pack is around 10MB, no huge data).

The rest of work is more pleasant. It’s not lightning fast (as I expected for the first time – I know it’s not RAM, beat me :) ), but your not waiting for the disk too much. And maybe it’s just me, but I feel more relaxed when the machine waits for me not vice versa. :D

If you have any questions or you want me to test something, let me know, I’ll try my best. Oh, for true geeks, I’m running SSDSA2M160G2GC (model code) or SSDSA2MH160G2XX (product code) Intel X25-M 34nm SATA SSD.

New SSD (Intel X25-M) – first feelings

It is couple of hours I’m using my new SSD – Intel X25-M. I ordered it to improve and speedup by working environment.

I’ll start with the migration steps I performed. My initial idea was to use “Complete PC Backup and Restore” function that’s available in Windows 7 (since Windows Vista, I think). As the hardware, except the disk, will be the same it should work smoothly. Research on some blogs and also some friends confirmed this idea (especially Michal Altair Valášek). You create a backup, swap disks, boot from DVD and start restore. Simple and also nice, that such a tool is available directly in Windows.

That was the original idea, and I did the backup just in case and also another two (you know, it’s my data). But my disk came with something called Apricorn Notebook Hard Drive Upgrade Kit. Basically it’s a USB bay for disks and bootable (not only) CD with some rebranded version of Acronis True Image (as I’ve found in some discussions). This tool, among other, can do the disk-to-disk copy. After the first boot and doing nothing just looking what it can do I decided to use it. If you’re asking why, it’s probably because it looked as simpler and more straightforward way. After some time the operation was done.

After boot the system found the drive, installed drives and asked me for reboot. Nothing bad (so far) happened.

Now about the performance. I state beforehand, that I’m using the drive only a while and I don’t have any numbers and I’m going to do any tests.

The first think that literally made me confused was the noise. Well the lack of any noise. Really, the drive is completely silent. And for a guy used to hear the disks from various notebooks, computers with and without cases, NASes for many years it’s really weird. I was so confused I don’t hear the heads seeking a turned off the stereo and looked for some problem. :) On the other hand it’s nice to have completely silent notebook (except times the fan is on).

The boot of Windows 7 was little faster, but nothing killing speed. Same after logging in. When you start application you can definitely feel it’s faster, especially application doing a lot of I/O, like Opera, Outlook or VMWare Workstation when starting VM. Roughly two times faster. But to be honest it’s not a high order of magnitude I was expecting (maybe I expected it to be close to RAM).

Anyway it’s just couple of hours. I’m pleased with the result so far. The drive is completely silent. It’s perceivable faster. And the battery life should be better too. I’ll give it hard times later while doing real work and report back.