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の2つの名前空間があるとします。

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

適応コード:

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 に答える