Search code examples
fluent-nhibernateautomapping

how to tell flunet-nhibernate to use the ID from a base class (which is abstract and ignored in the mapping)


i have an abstract class

public abstract class Document
{  
public int DocumentID {get; set;}
}

and derived class

public class DoctorDocument : Document{
public string DoctorName {get;set;}
}

and I'm using Fluent Auto Mapping,
i need not to make a table for Document, but i need each derived class to get the DocumentID as Primary Key.

 mappings.IgnoreBase<Document>();
 mappings.AddEntityAssembly(typeof(DoctorDocument).Assembly);
 mappings.Setup(c=>c.FindIdentity = type.Name == type.DeclaringType.Name + "ID";);

but it still can't find the ID and tells me that DoctorDocument doesn't have an ID. but when i made the following override it worked:

public class DoctorDocumentMap: IAutoMappingOverride<DoctorDocument>
    {
    public void Override(AutoMapping<DoctorDocument> mapping)
        {
        mapping.Id(x => x.Id, "DocumentID").GeneratedBy.Identity();
        }
}

how can i tell the automapping to do that for all entities?? especially the GeneratedBy.Identity();


Solution

  • Overriding DefaultAutomappingConfiguration might help.

    Something like this might work :

    public class MyAppAutoConfiguration : DefaultAutomappingConfiguration
    {
        public override bool IsId(Member member)
        {
            return "DocumentID" == member.Name;
        }
    }
    

    The configuration can be like this:

     var cfg = new MyAppAutoConfiguration();
            var autoPersistenceModel = AutoMap.AssemblyOf<Person>(cfg).IgnoreBase<Document>();
            ISessionFactory sessionFactory = Fluently.Configure()
                .Database(OracleClientConfiguration.
                Oracle10.ConnectionString(
                    ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString))
                 .Mappings(m =>
                    m.AutoMappings
                      .Add(autoPersistenceModel))
                  .BuildSessionFactory();