我开始使用Fluent.NHibernate为我的C#web probject正确使用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
}
并且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;
}
}
...
}
进入我的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>
我需要你的帮助:) 我的代码有什么问题?我该怎么做才能纠正我的代码?
非常感谢大家! :)
洛伦佐
答案 0 :(得分:0)
问题在于映射,您正在使用KeyProperty映射复合键,并且总是会抛出该错误,请改用KeyReference,并且应该可以正常工作
CompositeId()
.KeyReference(x => x.key.applicationId, "APPLICATION_ID")
.KeyReference(x => x.key.userId, "USER_ID")
.KeyReference(x => x.key.id, "ID");