从里到外创建数据库

时间:2012-12-10 18:04:13

标签: 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(),
    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);

由于某种原因,数据库不是为上面的dll(Bla.Di.Domain)创建的,它有一个像这样的简单类:

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>();
           };
    }
}

我添加了对域模型的引用(不同的名称空间)并更改了:

var mappings = AutoMap.AssemblyOf<Bla>(new AutomappingConfiguration());

为:

var mappings = AutoMap.AssemblyOf<Test>(new AutomappingConfiguration());

1 个答案:

答案 0 :(得分:0)

我们假设我们有两个名称空间:CompanyName.X和CompanyName.Y。

CompanyName.X包含A类和CompanyName.Y包含B,其中A和B继承自Entity。然后这种适应有助于:

原件:

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;
    }
    ...

改编代码:

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;
    }
    ...