1
votes

I have the following situation:

AssemblyX with db entities refers external assemblyY and use its classes as base classes. AssemblyX adds some navigation properties if necessary.

Of course I can copy all this entites and use automapper to solve my problem, but I have a reason to inherit existent entities.

So, I have Foo {} class in assemblyY and create Bar: Foo {} in assemblyX. Configure fluent automapping to ignore Foo (.IgnoreBase<Foo>()). Everything works good. But assemblyY has another entity Rab (that is covered by fluent automapping) that has a navigation property that refers to Foo. That cause an exception: "An association from the table Rab refers to an unmapped class: Foo".

public class Foo // .IgnoreBase<Foo>() - I require only derived Bar in DB
{
    public virtual Guid Id {get; set;}        
}

public class Bar: Foo
{
    public virtual Guid Id {get; set;}
    // Some new properties
}

public class Rab
{
    public virtual Guid Id {get; set;}
    public virtual Foo Foo // reference to unmapped class, I could not change type to Bar (external assembly)
}

How can I solve it. I tried to inherit the Rab and create new Foo property with Bar type, but unsuccessfully.

Thanks in advance for any advice.

1

1 Answers

0
votes

You can use a custom IAutomappingConfiguration to determine which type or member should be mapped :

class MyConfig : DefaultAutomappingConfiguration
        {
            public override bool ShouldMap(Type type)
            {
                return  !(typeof(Foo)).IsAssignableFrom(type);
            }

            public override bool ShouldMap(Member member)
            {
                return  !(typeof(Foo)).IsAssignableFrom(member.PropertyType);                }
        }

Then you can use your config like this :

AutoPersistenceModel model = new AutoPersistenceModel(new MyConfig());

And Finally :

Fluently.Configure()

                    .Mappings(m => m.UsePersistenceModel(model))
                    .BuildSessionFactory();