Trying to get my head round automapping. I am having a problem when trying to automap my domain and generate the database. Im sure its something simple im doing wrong.
The problem is, the correct tables are generated, but only the ID field from the base class is present within the generated tables, none of the other fields within the entities are generated.
BaseEntity is in a different namespace to the entities.
Im not sure where to go from here, any ideas?
Here is my mapping configuration:
public static ISessionFactory CreateSessionFactory()
{
return _sessionFactory = Fluently.Configure()
.Database(ConfigureDatabase())
.Mappings(m =>开发者_JS百科; m.AutoMappings.Add(CreateMappings()))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static IPersistenceConfigurer ConfigureDatabase()
{
return MsSqlConfiguration
.MsSql2008.ShowSql()
.ConnectionString(c => c.FromAppSetting("MSSqlConnectionString"))
.ProxyFactoryFactory<ProxyFactoryFactory>();
}
private static AutoPersistenceModel CreateMappings()
{
return AutoMap.AssemblyOf<Organisation>(new AutomappingConfig())
.Conventions.Add<CascadeConvention>();
}
private static void BuildSchema(Configuration config)
{
new SchemaUpdate(config)
.Execute(false,true);
}
Heres my autoMappingConfig
public class AutomappingConfig : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.Namespace == "Domain.Model" && type.IsClass;
}
}
And all of my entities inherit this base class:
public class BaseEntity<T> where T : BaseEntity<T>
{
public virtual int Id { get; set; }
}
And an example entity:
public class Contact : BaseEntity<Contact>, IAggregateRoot
{
public virtual String Name { get; set; }
public virtual Organisation Organisation { get; set; }
}
BaseEntity is in a different namespace to the entities.
What you may want to do is try putting them in the same namespace. Also you may want to comment out the check you have above:
return type.Namespace == "Domain.Model" && type.IsClass;
The above statement may be what is causing you to only map your base classes. What namespace is your base class in and what namespace are one of your example entities in?
When I run into problems like this I try to simplify things until I find out what is causing my issues.
Edit:
After further research I think you need to use the following:
.IgnoreBase(typeof(BaseEntity<>));
So your code above would be changed to:
private static AutoPersistenceModel CreateMappings()
{
return AutoMap.AssemblyOf<Organisation>(new AutomappingConfig())
.Conventions.Add<CascadeConvention>()
.IgnoreBase(typeof(BaseEntity<>));
}
This is an excerpt taken from the FNH Wiki:
We've added the IgnoreBase call which simply instructs the automapper to ignore the Entity class; you can chain this call as many times as needed.
With this change, we now get our desired mapping. Entity is ignored as far is Fluent NHibernate is concerned, and all the properties (Id in our case) are treated as if they were on the specific subclasses.
From Automapping: base-type as an inheritance strategy:
Abstract base-classes
You'll notice that our Entity class is abstract. This is good practice, but for the record, it is not mandatory. If you're experiencing problems, it's unlikely to be this.
In case you're wondering, making the class abstract is like saying "I'll never create this directly, instead I will create derived classes such as Customer and Order (which inherit from Entity)."
The default behavior is to consider abstract classes as layer supertypes and effectively unmapped, you may want to change this for specific scenarios. The easiest way to do this is to use IncludeBase, where T is your entity.
AutoMap.AssemblyOf<Entity>(cfg)
.IncludeBase<BaseEntity>();
AutoMap.AssemblyOf<Entity>(cfg)
.IncludeBase(typeof(BaseEntity<>));
Article: Auto-mapping generic base classes
精彩评论