How to have Fluent NHibernate automap a class inheriting from an abstract class inheriting from an interface?
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 t开发者_Go百科he IDataEntity type. What am I missing here? I'd really like all the classes in my domain to inherit from IDataEntity.
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.
精彩评论