Bug when automapping subclasses - code attached

James Spibey's Avatar

James Spibey

15 Apr, 2010 08:18 AM via web

Hi,

I'm having a problem when attempting to automap a model which contains subclasses. The error manifests itself as

Tried to add property 'NPC' when already added.

I've stepped through the code and it looks like it's trying to map the base class of the hierarchy twice which is causing the error. Test project (in VB) attached which reproduces the error against build 636.

Any troubleshooting help would be much appreciated

Thanks

James

  1. 2 Posted by James Spibey on 20 Apr, 2010 07:54 AM

    James Spibey's Avatar

    Still struggling with this one - it looks like it's either mapping the base class twice or that it's failing to identify the subclasses as actual subclasses.

    Can anyone help?

  2. 3 Posted by James Spibey on 21 Apr, 2010 10:29 AM

    James Spibey's Avatar

    I posted a reply to this yesterday but it seems to have been lost. I found one bug in AutoMapper::MapInheritanceTree() where it seems to overwite whatever might have been set as the type's discriminator with the default on.

    So this

                if (isDiscriminated && !discriminatorSet && mapping is ClassMapping)
                {
                    var discriminatorColumn = expressions.DiscriminatorColumn(classType);
                    var discriminator = new DiscriminatorMapping
                    {
                        ContainingEntityType = classType,
                        Type = new TypeReference(typeof(string))
                    };
                    discriminator.AddDefaultColumn(new ColumnMapping { Name = discriminatorColumn });
    
                    ((ClassMapping)mapping).Discriminator = discriminator;
                    discriminatorSet = true;
                }
    

    Should probably be something like

                if (isDiscriminated && !discriminatorSet && mapping is ClassMapping)
                {
                    if(((ClassMapping)mapping).Discriminator == null)
                    {
                        var discriminatorColumn = expressions.DiscriminatorColumn(classType);
                        var discriminator = new DiscriminatorMapping
                        {
                            ContainingEntityType = classType,
                            Type = new TypeReference(typeof(string))
                        };
                        discriminator.AddDefaultColumn(new ColumnMapping { Name = discriminatorColumn });
    
                        ((ClassMapping)mapping).Discriminator = discriminator;
                    }
    
                    discriminatorSet = true;
                }
    

    However, this still hasn't fixed the primary problem - can anyone help with that?

  3. 4 Posted by James Spibey on 14 May, 2010 11:03 AM

    James Spibey's Avatar

    Sorry to keep banging on about this but can anyone offer any help with this problem?

  4. Support Staff 5 Posted by James Gregory on 16 May, 2010 04:44 PM

    James Gregory's Avatar

    Hi James, sorry for not getting back to you sooner. Have you by any chance tried the most recent build? We've put in quite a few automapping fixes since you originally asked your question.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is five times five?