Monthly Archives: September 2009

Unique constraint on MS SQL?!

I have a strong feeling that MS SQL knows I don’t like it. I don’t say it’s a bad database, but sometimes it’s really “interesting”. ;)

OK, so what I faced this time? Let’s start with basic facts. The NULL value in database is a special value. NULL and NULL is again NULL, NULL and False is again NULL and NULL isn’t equal to NULL. Two days ago I was creating a chain of records in database (which isn’t too relationalish, but …) and I needed unique constraint on one column. No problem you may think. So did I. But not in MS SQL.

Check this example:

1> create table test(id int primary key, foo int);
2> create unique index idx_text on test(foo);
3> go
1> insert into test values (1, null);
2> go

(1 rows affected)
1> insert into test values (2, null);
2> go
Msg 2601, Level 14, State 1, Server X2-001SQLEXPRESS, Line 1 Cannot insert duplicate key row in object 'dbo.test' with unique index 'idx_text '.
The statement has been terminated.

What a mess! Looks like somebody in MS SQL engine team thinks NULL == NULL. After some railing, testing on Firebird and asking my friend about Oracle, I googled: https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=299229. Looks like I’m not alone; feeling better. ;)
Luckily I’m working on MS SQL 2008 and I can use filtered indices. Created unique index with where clause where <column> is not null and I’m done. But boy that was a bitter finding. Hope I’ll not be punched to the face with these basics again in an at least two months.

FirebirdClient and InterBase

Wondering what changed on Earth in last, say, four weeks. I’m getting more than average (read: more than zero) emails, asking whether FirebirdClient works with InterBase. Similarly in mailing list.

Well, the short answer is no. The long: FirebirdClient is created as part of Firebird project. Hence it’s focused on Firebird. And even both Firebird and InterBase have same history, now the engines, and more in future, are more and more different. So it may work (with some tweaks) with current versions of InterBase, but it’s tested with (sponsored by, focused on) Firebird only.

Astoria 1.5 CTP2 – projections and counts

CTP2 for Astoria 1.5 has been released some days ago and this version comes with couple of new features. You can read about the list on http://blogs.msdn.com/adonet/archive/2009/09/01/ado-net-data-services-v1-5-ctp2-now-available.aspx. For me, the projections support and count support are the most exciting.

The lack of projections support was a pain if you had some big blob fields in your model. I sketched little workaround here. But since this version you can use the $select operator to get only columns you’re interested in. You’ll write for instance http://foobar/Service.svc/Masters?$select=ID,Name,Details, and you get back object with properties ID, Name and collection Details (for simple master-detail example). So you can easily get data except already mentioned big blob fields.

The other new feature is support for count. You can simply ask Astoria to return number of entities in particular query. You can use it without any filtering etc. i.e. http://foobar/Service.svc/Masters/$count or with other operators as well, i.e. $filter: http://foobar/Service.svc/Masters/$count?$filter=Name eq 'rrr'. So you don’t have to fetch all the data returned just to get the final number. And that’s not all. You can get count of entities in result also inline. With keyword $inlinecount you get the result and the count as well. The query http://foobar/Service.svc/Masters?$inlinecount=allpages&$top=2 will return at most two entities and count of all. On the other hand the http://foobar/Service.svc/Masters?$inlinecount=allpages&$filter=ID lt 5&$top=2 will return count of entities where ID < 5 and only two of these. Simply the $inlinecount is applied to whole result after all $filters have been applied. This inline count is pretty nice especially for classic “list of products” view with paging not to overload the user.

Looking forward to see the new version of Astoria, Astoria “offline” and Entity Framework v4 all released.

First touches on Code Only in EF4

I finally got my hands on the new feature in EF4, Code Only. It’s now available in feature CTP. What’s the code only? Very shortly you write just a code and express your mapping in code as well. There’s no model, no edmx file, no CSDL, MSL, SSDL files.

The current version is pretty limited, it’s more preview than something you can do some work with (which I wasn’t aware of and I was little disappointed). You can read more about what will be available on http://blogs.msdn.com/efdesign/archive/2009/06/10/code-only.aspx, http://blogs.msdn.com/efdesign/archive/2009/08/03/code-only-enhancements.aspx and http://thedatafarm.com/blog/data-access/next-version-of-ef-code-only-design-laid-out-by-ms/.

I mainly wanted to try it on a non standard (read: not “follow this demo”) MS SQL database and on Firebird as well. The first bad news is, that current CTP supports only SqlClient. And probably some following previews will too, as the other providers model will be built on top of it (but I’ll try to push this as much as I can, to see some Firebird demo soon). The other is, that the currently available version is limited, even features in first Code Only blog post are not working. :( Right now you can only create a context and use it for work with all defaults – default names for tables, default mapping etc.

using (SqlConnection conn = new SqlConnection(@"Data Source=.sqlexpress;Initial Catalog=testovaci;Integrated Security=True;Pooling=False"))
{
	ContextBuilder<COContext> builder = new ContextBuilder<COContext>();
	COContext context = builder.Create(conn);
	string script = context.CreateDatabaseScript();
	context.Masters.ToArray();
}

Nothing special. If you try it with FirebirdClient, you’ll get NotSupportedException immediately. Dammit.

using (FbConnection conn = new FbConnection("database=localhost:rrr.fdb;user=sysdba;password=sysdba"))
{
	ContextBuilder<COContext> builder = new ContextBuilder<COContext>();
	builder.Create(conn);
}

Despite the frustration I found couple of new interesting extension methods (in Microsoft.Data.Objects, also ContextBuilder sits there). One of these is CreateDatabaseScript. This method works not only with Code Only created ObjectContext, but on every ObjectContext. So you can create your database creation script during runtime even if you’re using i.e. EDMX file easily. I’m also seeing great opportunity for this with Code Only setup during runtime. You can have different builds with different setups and create script for users based on selection. And by the way, there’s also CreateDatabase method available. But again right now works only with MS SQL only. But I know other databases will be supported as well, I have tested some pieces already.

I like the Code Only feature, maybe more than Model First (probably because I’m using ERD tools to model my databases). And I’m looking forward to see more progress on it, and also the model for 3rd party providers.

FirebirdClient 2.5.1 released

I’m proud to announce next version of FirebirdClient – 2.5.1. This release is focused mainly on bug fixes, no new features were added.

You can see all fixed issues from tracker. And download it from http://firebirdsql.org/index.php?op=files&id=netprovider.

Thanks to all people who reported any issues for trying to make FirebirdClient better.

Astoria “offline” and Firebird

Few weeks ago I was playing (and I’m still about to continue playing) and writing about Astoria offline. I setted up some challenge for me to try to make it work with Firebird (as the big database, SQL CE still as local store). It looked like it should be plausible. But it’s not. :)

After creating model from database, creating and rewriting sql scripts for Firebird, I tried to use this model in Astoria offline. Here I hit the wall. After couple of hours I gave up and talked with Pablo Castro, who did much of the work in Astoria offline. He confirmed that there’s no particular check and reject other databases, but with this “offline preview” there’s a lot of assumption for MS SQL Server.

Never mind, when another test version will be available be sure, I’ll try it and we’ll see what’s what. :)