0

我通常没有问题通过 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());
4

1 回答 1

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;
    }
    ...
于 2012-12-11T18:19:54.877 回答