views:

250

answers:

1

Right now, I'm switching my project over from the classic fluent nhibernate style of manually defining a ClassMap for each domain entity, to having the auto-mapper auto-generate the mappings for me. But I'd like to keep using the classes I've already mapped in the classic style, until I can tweak the automappings to match the old classic mappings.

The only problem is that fluent nhibernate crashes when the auto mapper hits a class that's already been mapped in the classic fluent nhibernate style.

Here's my AutoPersistenceModel setup code:

_autoPersistenceModel = AutoMap.AssemblyOf<DomainEntity>();
_autoPersistenceModel.AddMappingsFromAssembly(typeof (PlayerPersistenceMap).Assembly);

_autoPersistenceModel.Alterations(x =>
                    x.Add<AutoMappingAlteration>())
                .Setup(s =>
                {
                    s.FindIdentity = type => type.Name == "ID";

                    s.IsBaseType = type => (type == typeof(DomainEntity));
                })
                .Conventions
                    .AddFromAssemblyOf<IdentityColumnConvention>()
                    .UseOverridesFromAssemblyOf<PlayerMappingOverride>();

Can anyone help me out here?

More Info:

I also tried the technique mentioned on the fluent-nhibernate wiki here. Alas I'm still getting the error: Duplicate class/entity mapping.

+2  A: 

The mixed fluent mappings and auto mappings example in the wiki should work, if it doesn't then there's a bug.

As a work-around, exclude the types that have been manually mapped from you automappings. You'd do that by using the Where method, as show in the wiki examples, something like this:

AutoMap.AssemblyOf<DomainEntity>()
  .Where(type => type != typeof(OneOfYourManuallyMappedClasses));

If you have a lot of fluent mappings, you could create a collection to keep the automapping setup clean:

var mappedTypes = new[] { typeof(One), typeof(Two) };

AutoMap.AssemblyOf<DomainEntity>()
  .Where(type => !mappedTypes.Contains(type));

Again, this shouldn't be necessary, but if you're certain it's not working with the wiki example then I'd suggest raising an issue.

James Gregory
I did discover a bug in my SchemaUpdate related code, which could have been triggering the error message, but I'm not sure if they are related. I'll test it again today with the fix, and make a comment here if the problem is still there. Thanks for your input.
Mark Rogers
Hey, I went back and double checked, and if I don't do a workaround, then I still get the error "Duplicate class/entity mapping"
Mark Rogers