views:

561

answers:

4

I'm having problem with SubSonic 3 and multiple Primary Key columns. It seems like it isn't supported via the ActiveRecord T4 script.

In line 173 of Settings.ttinclude

return this.Columns.SingleOrDefault(x=>x.IsPK) ?? this.Columns[0];

It tries to get a Single Primary Key Column and failed.

Any solutions?

+3  A: 

Many ORM products do not support composite keys due to the overwhelming complexity of doing so. As far as I know, NHibernate is the only well-known .Net ORM product that does.

Mindscape was discussing composite key support for version 3 of their Lightspeed product, but I don't know too much about it.

SubSonic does not currently support composite keys.

womp
Linq-to-SQL supports composite keys, and in fact requires them for many-to-many mappings.
Timwi
A: 

You could potentially create a view which creates a primary key by concatenating the composite key columns. e.g. if they're varchar columns PK = COALESCE(K1, '|', K2, '|', K3). If they're numeric, you could do something similar by multiplying each key column by a multiplier to create a unique PK.

Jon Galloway
Gosh, what a hack!
Timwi
Yes, it's a hack - I only proposed it in case you were really stuck. However, I'd call a table with multiple PK's a hack, too.
Jon Galloway
A: 

I believe EntitySpaces does support them.

My 2cents-

-- this was intented to be an answer to womp just to say that NHibernate isn't the only well known ORM supporting composite keys. At least leave a comment (or an insult :p ) when downvoting --

elpipo
+2  A: 

I'll tweak the templates to add support for this in the future (since a lot of people are having issues with it) but you can change this:

return this.Columns.SingleOrDefault(x=>x.IsPK) ?? this.Columns[0];

to this:

return this.Columns.Where(x=>x.IsPK).ToArray();

(this is free-handed) and then change the return type to Column[]. The change should be pretty simple from this point of view, but then you'd need to change the templates throughout.

I know people like composite keys - and they are particularly important for many/many, but (my opinion) I don't like the design. A table (that's not many/many) should have one PK to uniquely ID a row...

I also understand that many folks can't control such a thing :). Anyway - if you'd like to help and fork/push this, I'd really appreciate it.

Rob Conery
Having just looked at the pain of this issue (trying to fix the templates), I would suggest either: use NHibernate OR use a single PK.Changing the above lines of code is a waste of your time: it still won't do what you want.
cmroanirgo
I try to modify template but it not work, it require to modify many classes in SubSonic too. I can say this is worst of time, put SubSonic down and go with NHibernate.
In The Pink