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.

Thanks,
Mike

file attachments

Closed Sep 16, 2014 at 10:38 PM by iLMaX

comments

toolcontrol wrote Aug 21, 2013 at 10:38 PM

Forgot to mention I am using Visual Studio 2012.

toolcontrol wrote Feb 20, 2014 at 5:44 PM

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).

andym1978 wrote Sep 9, 2014 at 11:13 PM

Please share. I would like to see your solution.

wrote Sep 10, 2014 at 1:41 AM

toolcontrol wrote Sep 10, 2014 at 1:41 AM

Starting with version 74006 I changed two files twice (once for VS2012 version, once for VS2013 version).

The changes (from VS2013 version):

File: Source/Dsl/CustomCode/DomainClasses/ConfigurationElementCollection.cs

Replace line 69 with:
            // If using hash for element key it doesn't care how many key properties there are.
            if (this.UseHashForElementKey)
                return;
            if (numKeys == 0)
File: Source/DslPackage/TextTemplates/ConfigurationSectionDesignerSchema.tt is attached.

The changes are virtually identical for the VS 2012 version.

iLMaX wrote Sep 10, 2014 at 9:33 AM

I think the simplest solution is to avoid any properties as a key property and set use hash for key to true.

No code change required.

toolcontrol wrote Sep 10, 2014 at 4:11 PM

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?

andym1978 wrote Sep 11, 2014 at 4:31 PM

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.

andym1978 wrote Sep 16, 2014 at 8:17 PM

NOTE: It might be nice to include text in the error msg stating that hash key is an optional solution.

iLMaX wrote Sep 16, 2014 at 9:13 PM

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.

@andym1978 absolutely.

iLMaX wrote Sep 16, 2014 at 10:34 PM

Enhanced validation message with 75421

wrote Sep 16, 2014 at 10:38 PM

wrote Sep 16, 2014 at 10:38 PM