Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top