Nhibernate 由内而外创建数据库
我通常通过NHibernate“播种”我的数据库不会有问题,如下所示:Nhibernate 由内而外创建数据库,nhibernate,fluent-nhibernate,s#arp-architecture,Nhibernate,Fluent Nhibernate,S#arp Architecture,我通常通过NHibernate“播种”我的数据库不会有问题,如下所示: ... string[] mappingAssemblies = new string[] { "Bla.Di.Domain" }; string configFile = "NHibernate.config"; NHibernate.Cfg.Configuration config = NHibernateSession.Init( new SimpleSessionStorage(), mappingA
...
string[] mappingAssemblies = new string[] { "Bla.Di.Domain" };
string configFile = "NHibernate.config";
NHibernate.Cfg.Configuration config = NHibernateSession.Init(
new SimpleSessionStorage(),
mappingAssemblies,
new AutoPersistenceModelGenerator().Generate(),
configFile);
TextWriter writeFile = new StreamWriter("d:/SeedSQL.sql");
var session = NHibernateSession.GetDefaultSessionFactory().OpenSession();
new SchemaExport(config).Execute(true, true, false, session.Connection, writeFile);
namespace Bla.Di.Domain
{
using SharpArch.Domain.DomainModel;
public class Test : Entity
{
public virtual string X { get; set; }
}
}
出于某些原因,没有使用以下简单类为上述dll(Bla.Di.Domain)创建数据库:
...
string[] mappingAssemblies = new string[] { "Bla.Di.Domain" };
string configFile = "NHibernate.config";
NHibernate.Cfg.Configuration config = NHibernateSession.Init(
new SimpleSessionStorage(),
mappingAssemblies,
new AutoPersistenceModelGenerator().Generate(),
configFile);
TextWriter writeFile = new StreamWriter("d:/SeedSQL.sql");
var session = NHibernateSession.GetDefaultSessionFactory().OpenSession();
new SchemaExport(config).Execute(true, true, false, session.Connection, writeFile);
namespace Bla.Di.Domain
{
using SharpArch.Domain.DomainModel;
public class Test : Entity
{
public virtual string X { get; set; }
}
}
有没有可能出问题的地方?我没有例外。我在“种子”项目中引用了dll。我的dll的文件位置可能有问题(这是一个相当复杂的解决方案)。谢谢
PS(根据评论中的要求):
这是我的AutoPersistenceModelGenerator-请注意,它位于另一个命名空间中:
public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
{
public AutoPersistenceModel Generate()
{
//var mappings = AutoMap.AssemblyOf<Bla>(new AutomappingConfiguration());
var mappings = AutoMap.AssemblyOf<Test>(new AutomappingConfiguration());
mappings.IgnoreBase<Entity>();
mappings.IgnoreBase(typeof(EntityWithTypedId<>));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
return mappings;
}
private static Action<IConventionFinder> GetConventions()
{
return c =>
{
c.Add<PrimaryKeyConvention>();
c.Add<CustomForeignKeyConvention>();
c.Add<HasManyConvention>();
c.Add<TableNameConvention>();
};
}
}
公共类AutoPersistenceModelGenerator:IAutoPersistenceModelGenerator
{
公共自动持久性模型生成()
{
//var mappings=AutoMap.AssemblyOf(新的AutomappingConfiguration());
var mappings=AutoMap.AssemblyOf(新的AutomappingConfiguration());
mappings.IgnoreBase();
IgnoreBase(typeof(EntityWithTypedId));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf();
返回映射;
}
私有静态操作GetConventions()
{
返回c=>
{
c、 添加();
c、 添加();
c、 添加();
c、 添加();
};
}
}
我添加了对域模型的引用(不同的名称空间),并更改了:
var mappings = AutoMap.AssemblyOf<Bla>(new AutomappingConfiguration());
var mappings=AutoMap.AssemblyOf(新的AutomappingConfiguration());
致:
var mappings=AutoMap.AssemblyOf(新的AutomappingConfiguration());
假设我们有两个名称空间:CompanyName.X和CompanyName.Y
CompanyName.X包含类A,CompanyName.Y包含B,其中A和B从实体继承。然后这种适应有助于:
原件:
public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
{
public AutoPersistenceModel Generate()
{
var mappings = AutoMap.AssemblyOf<A>(new AutomappingConfiguration());
mappings.IgnoreBase<Entity>();
mappings.IgnoreBase(typeof(EntityWithTypedId<>));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
return mappings;
}
...
公共类AutoPersistenceModelGenerator:IAutoPersistenceModelGenerator
{
公共自动持久性模型生成()
{
var mappings=AutoMap.AssemblyOf(新的AutomappingConfiguration());
mappings.IgnoreBase();
IgnoreBase(typeof(EntityWithTypedId));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf();
返回映射;
}
...
改编代码:
public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
{
public AutoPersistenceModel Generate()
{
var mappings = AutoMap.AssemblyOf<A>(new AutomappingConfiguration());
mappings.AddEntityAssembly(typeof(CompanyName.Y.B).Assembly);
mappings.IgnoreBase<Entity>();
mappings.IgnoreBase(typeof(EntityWithTypedId<>));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();
return mappings;
}
...
公共类AutoPersistenceModelGenerator:IAutoPersistenceModelGenerator
{
公共自动持久性模型生成()
{
var mappings=AutoMap.AssemblyOf(新的AutomappingConfiguration());
映射.AddEntityAssembly(typeof(CompanyName.Y.B).Assembly);
mappings.IgnoreBase();
IgnoreBase(typeof(EntityWithTypedId));
mappings.Conventions.Setup(GetConventions());
mappings.UseOverridesFromAssemblyOf();
返回映射;
}
...
我认为问题与AutoPersistenceModelGenerator有关,它位于不同的命名空间中。还不确定要做什么…粘贴AutoPersistenceModelGenerator的代码