0

私は、すでに NHibernate を正しく使用している C# Web プロジェクトの Fluent.NHibernate から始めていますが、今は Fluent に移行したいと考えています。

構成リクエストを実行しようとすると、次のエラーが表示されます。

SessionFactory の作成中に、無効または不完全な構成が使用されました。詳細については、PotentialReasons コレクションと InnerException を確認してください。

  • データベースが Database メソッドで構成されていません。

PotentialReason プロパティに次のように表示されます。

データベースが Database メソッドで構成されていません。

InnerException に InnerException (xD) に

複合 ID クラスは Equals() をオーバーライドする必要があります: it.quasar.core.libraries.entities.Config

しかし、Fluent から NHibernate に変更すると (従来の hbm ファイルを使用し、Config クラスを変更せずに)、問題は発生しません。したがって、Fluent ライブラリを使用すると問題が発生します。

コードがこの命令を実行しようとすると、エラーが表示されます。

this._sessionFactory = this._configuration
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TIdentifier>())
                .BuildSessionFactory();

現時点では、エンティティ (Config.cs) を 1 つだけ移行しようとしましたが、これが私のエンティティです。

public class ConfigKey : BaseKey
{
    public virtual string id { get; set; }

    public ConfigKey() : base()
    {
        if(this.userId == null)
        {
            this.userId = "SYSTEM";
        }
    }

    #region OVERRIDES
    public override bool Equals(object obj)
    {
        if(obj != null)
        {
            ConfigKey _key = obj as ConfigKey;
            if(_key == null)
            {
                return false;
            }

            if(_key.id == this.id && _key.applicationId == this.applicationId && _key.id == this.id)
            {
                return true;
            }
        }

        return false;
    }

    public override int GetHashCode()
    {
        return (this.id.ToString() + "|" + this.applicationId.ToString() + "|" + this.id)
            .GetHashCode();
    }
    #endregion
}

public class Config : BaseEntity<ConfigKey>
{
    public virtual string value { get; set; }
    public virtual string description { get; set; }

    #region CONSTRUCTORS
    public Config() { }

    public Config(ConfigKey key)
    {
        this.key = key;
    }
    #endregion
}

public abstract class BaseKey
{
    public virtual Guid applicationId { get; set; }
    public virtual string userId { get; set; }

    public virtual bool isNew
    {
        get
        {
            bool test = false;

            Dictionary<string, ObjectDefinition> transposed = this.Transpose();
            foreach (KeyValuePair<string, ObjectDefinition> property in transposed)
            {
                if (property.Value.Value == null)
                {
                    test = true;
                    break;
                }
                else if (property.Value.Default != null && (property.Value.Value.ToString() == property.Value.Default.ToString()))
                {
                    test = true;
                    break;
                }
            }

            return test;
        }
    }

    public BaseKey()
    {
        this.applicationId = BaseContext.getContext.applicationId;

        try
        {
            //if exists a user context, take the user id
            this.userId = BaseContext.getContext.user.key.id;
        }
        catch
        {
            //else user id is not setted
            this.userId = null;
        }
    }

    #region OVERRIDES
    public override bool Equals(object obj)
    {
        if (obj != null)
        {
            BaseKey _key = obj as BaseKey;
            if (_key == null)
            {
                return false;
            }

            if (_key.applicationId == this.applicationId && _key.userId == this.userId)
            {
                return true;
            }
        }

        return false;
    }

    public override int GetHashCode()
    {
        return (this.applicationId.ToString() + "|" + this.userId)
            .GetHashCode();
    }
    #endregion
}

マップ クラスは次のとおりです (Config と ConfigMap は同じ名前空間と同じアセンブリにあります)。

public class ConfigMap : ClassMap<Config>
{
    public ConfigMap()
    {
        Schema("dbo");
        Table("CONFIG");

        CompositeId()
            .KeyProperty(x => x.key.applicationId, "APPLICATION_ID")
            .KeyProperty(x => x.key.userId, "USER_ID")
            .KeyProperty(x => x.key.id, "ID");

        Map(x => x.description, "DESCRIPTION");
        Map(x => x.value, "VALUE");
    }
}

これは SessionFactory です (私はこれを従来の NHibernate に使用しました。Fluent 用の別の名前空間にこの新しいものを作成しました):

public class NHibernateHelper<TIdentifier> where TIdentifier : class
{
    ISessionFactory _sessionFactory;
    FluentConfiguration _configuration;

    private ISessionFactory SessionFactory
    {
        get
        {
            if (this._sessionFactory == null)
            {
                if(this._configuration == null)
                {
                    this._configuration = Fluently.Configure();
                }

                this._sessionFactory = this._configuration
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TIdentifier>())
                    .BuildSessionFactory();
            }

            return this._sessionFactory;
        }
    }
...
}

私のweb.configには、NHibernateに使用したものと同じものが残っています:

  <!-- NHibernate configuration -->
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="it.quasar.NHibernate">
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <!--roperty name="connection.driver_class">NHibernate.Driver.OleDbDriver</property-->
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="connection.connection_string_name">lollo</property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>

あなたの助けが必要です :) 私のコードの何が問題なのですか? コードを修正するにはどうすればよいですか?

みんな、どうもありがとう!:)

ロレンツォ

4

1 に答える 1