There is an unsaved comment in progress. You will lose your changes if you continue. Are you sure you want to reopen the work item?

1

Closed

ConfigurationElementCollection item type has more than one key property

description

I've spent the last day and half with CSD and I've had pretty good luck converting some custom sections, elements, and element collections. My reason for pursuing this approach is as a workaround to a problem with xsd.exe that Microsoft says they don't
plan to fix (it is no longer possible to generate xsd file from an assembly containing custom configuration types).

I have a configuration element collection that holds instances of a configuration element type that has two key properties (this is intentional). I made this work previously by overriding GetElementKey on the collection class. It looks like CSD is trying to
do the same thing (it overrides GetElementKey and references a property that I can implement in a partial class. So it seems like it ought to work, but I can't get the project to compile. I get the following error messages:

Error 5 The Item Type of this collection has more than one key property.
Error 6 Running transformation: System.InvalidOperationException: Sequence contains more than one element

I can get both error messages to go away if I do not mark both properties as keys, but the generated code won't allow me to override GetElementKey. If I do not mark either properties as a key I can override GetElementKey but I cannot create a configuration
element collection that refers to an item type that doesn't have a key.

Any help would be appreciated. I can send more details if it will help, but I don't think it's difficult to reproduce this scenario.

comments

By the way, In order to work around this issue from last year I have customized versions of csd-74006 for VS2012 and VS2013 that allow multiple keys when "UseHashForElementKey" is selected - and generate more correct schema for add-remove-clear
maps.

If interested let me know and I'll post the changes (two small changes, same for VS2012 and VS2013).

I suppose that might work. I have a vague recollection that CSD didn't like creating a ConfigurationElementCollection with an item type that didn't have any keys (but that might be from an earlier version).

One suggestion: it should be possible to generate an implementation of GetHashCode using the identified key properties (not that I did this, it just occurred to me). That might make it easier for folks that want multi-field keys.

I haven't touched this in a while and given the lack of recent activity for CSD (and relatively small number of downloads) I kind of figured that CSD was fading away. I'm glad to see that someone is paying attention, is there any plan to enhance this tool further?

There are definite plans to improve this tool. Check out the discussion section to see more. We hit a point where all developers became very busy at work/life at the same time, but the situation has improved recently. The DSL framework and code generation
that this tool is based on has a steep learning curve for new developers, which can be a turn-off. I am developing some much needed documentation related to the source code of this tool, so I hope to get more contributions from the community after it is released.

Since Key property is a domain property of CSD model I think now is conceptually wrong to allow more than one key property on an element. As you pointed out, we could allow multiple keys property and use it for an implementation of the GetHasCode, btw
this is not implemented now, you actually have to implement this yourself in a partial class.

Now I'm currently a little bit reticent about changing the current code generator (it uses CodeDOM) because every addition is quite painful, what I would like to achieve is a sort of extensibility (as Entity Framework 4.0 does) where we provide you a default
code generator that can be easily customized to achieve the desired effect. This IMHO is a much more effective solution. You can find our plans about extensibility
here any feedback are appreciated.