How to have Fluent NHibernate automap a class inheriting from an abstract class inheriting from an interface?

StackOverflow https://stackoverflow.com/questions/1809909

  •  05-07-2019
  •  | 
  •  

Question

I've just started using Fluent NHibernate and have run into the following problem trying to automap my entities:

public interface IDataEntity {}

public abstract class PhysicalEntity : IDataEntity {

         public virtual int Id { get;  set; }
         public virtual string Name { get; set; }
}

public class Mine  : PhysicalEntity {

        public virtual string MineString { get; set; }
}

private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(c => c.FromConnectionStringWithKey("CSMID_FNH")))
                .Mappings(m =>
                m.AutoMappings.Add(
                AutoMap.AssemblyOf<Mine>()
                .Where(t => t.Namespace == "DAL.DomainModel" && t.IsClass && !t.Name.EndsWith("Attribute") )
                .IgnoreBase<PhysicalEntity>()))
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }

Now if I remove the reference to the IDataEntity interface, the automapping works. I tried inserting an ID field in the interface however this results in a NHibernate runtime error, as does telling the auto map to ignore the IDataEntity type. What am I missing here? I'd really like all the classes in my domain to inherit from IDataEntity.

Was it helpful?

Solution

Ok, so I think I have an answer.

All I had to do was modify my IDataEntity like so:

public interface IDataEntity { int Id { get; } }

I'd tried this originally with a get and set, but then I would have problems because my abstract class used a protected set and couldnt inherit from the interface. Leaving the setter out of the interface seems to be working for now, hopefully it doesnt introduce any other issues.

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