我从已经正确使用 NHibernate 的 C# web probject 开始使用 Fluent.NHibernate,现在我想迁移到 Fluent。
当我尝试执行配置请求时,我收到错误:
创建 SessionFactory 时使用了无效或不完整的配置。检查 PotentialReasons 集合和 InnerException 了解更多详细信息。
- 数据库不是通过数据库方法配置的。
进入PotentialReason属性,我看到:
数据库不是通过数据库方法配置的。
进入 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),这是我的实体:
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>
我需要你的帮助 :) 我的代码有什么问题?我该怎么做才能更正我的代码?
非常感谢大家!:)
洛伦佐