没有通过数据库方法配置流利的NHibernate数据库
本文关键字:数据库 NHibernate 配置 方法 | 更新日期: 2023-09-27 18:05:30
我从Fluent开始。我的c# web项目已经正确使用了NHibernate,现在我想迁移到Fluent。
当我尝试做一个配置请求时,我收到错误:
创建时使用了无效或不完整的配置SessionFactory。检查PotentialReasons集合和InnerException查看详细信息。
- 没有通过数据库方法配置数据库。
插入PotentialReason属性,我看到:
没有通过数据库方法配置数据库。
into InnerException into InnerException (xD) i see:
composite-id类必须重写Equals():
但当我从Fluent到NHibernate(使用经典hbm文件,不改变配置类),我没有看到任何问题…所以问题发生时,我使用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
}
和map类如下(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;
}
}
...
}
进入我的网络。配置我已经留下了相同的我使用的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>
我需要你的帮助。我的代码有什么问题?我能做些什么来纠正我的代码?非常感谢大家!:)
洛伦佐
问题是映射,你在用KeyProperty映射组合键这总是会抛出错误,用KeyReference代替,这应该是完美的
CompositeId()
.KeyReference(x => x.key.applicationId, "APPLICATION_ID")
.KeyReference(x => x.key.userId, "USER_ID")
.KeyReference(x => x.key.id, "ID");