Fluent NHibernate“数据库未通过数据库方法配置。”

问题描述 投票:0回答:1

我从已经正确使用NHibernate的C#Web probject的Fluent.NHibernate开始,现在我想迁移到Fluent。

[当我尝试执行配置请求时,我收到错误消息:

创建一个无效或不完整的配置时,SessionFactory。检查PotentialReasons集合和InnerException有关更多详细信息。

  • 未通过数据库方法配置数据库。

进入PotentialReason属性,我看到:

未通过数据库方法配置数据库。

我看到的将InnerException转换成InnerException(xD):

composite-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),这是我的实体:

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>

我需要您的帮助:)我的代码有什么问题?我该怎么做才能更正我的代码?

非常感谢大家! :)

洛伦佐

c# nhibernate fluent-nhibernate fluent-nhibernate-mapping nhibernate-configuration
1个回答
0
投票

问题是映射,您正在使用KeyProperty映射复合键,并且总是会抛出该错误,请改用KeyReference,并且应该可以正常工作

CompositeId()
   .KeyReference(x => x.key.applicationId, "APPLICATION_ID")
   .KeyReference(x => x.key.userId, "USER_ID")
   .KeyReference(x => x.key.id, "ID");
this should work perfect.
© www.soinside.com 2019 - 2024. All rights reserved.