0
votes

i'm starting with Fluent.NHibernate for my C# web probject that already uses NHibernate correctly and now i want to migrate to Fluent.

When i try to doing a config request i receive the error:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  • Database was not configured through Database method.

into PotentialReason property i see:

Database was not configured through Database method.

into the InnerException into InnerException (xD) i see:

composite-id class must override Equals(): it.quasar.core.libraries.entities.Config

but when i change from Fluent to NHibernate (using classic hbm file and without changing the Config class), i don't see any problems... so the problem occurs when i using Fluent library.

The error appears when the code try to execute this instruction:

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

At the moment I have tried to migrate just one entity (Config.cs), this is my entity:

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
}

and the map class is the following (Config and ConfigMap are into the same namespace and into the same assembly):

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

and this is the SessionFactory (i used it for classical NHibernate, i have created this new one in another namespace for 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;
        }
    }
...
}

into my web.config i have left the same that i used for 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>

I need your help :) What's wrong in my code? What can i do to correct my code?

Thanks a lot to everybody! :)

Lorenzo

1
Why add fluent? NHibernate already has mapping by code.Fran
really? do you have some docs about this feature?Lollo

1 Answers

0
votes

The problem is the mapping, you're mapping a composite key with KeyProperty and that is always going to throw that error, use KeyReference instead and that should work perfect

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