问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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 --
来源:https://stackoverflow.com/questions/1183595/subsonic-3-and-multiple-pk-columns