nhibernate:在ManyToOne属性中使用compositeId的部分 [英] nhibernate: using parts of the compositeId in a ManyToOne Property
问题描述
表实体
(
otherEntity_id int //主键
id int // primarykey
parent_id int
)
和class
public class实体
{
public OtherEntity其他{get;组; }
//简单的int来区分同一个OtherEntity的不同实体
public int Id {get;组; }
公共实体父{get;组; }
}
是否可以将这个映射到类实体? (如果是的话)
下面的代码会保存,因为dbcommand中没有足够的列,所以使用了两次:
pre $ CompositeId()
.KeyReference(e => e.Other,otherEntity_id)
.KeyProperty(e => e。 Id,id)
引用(e => e.Parent).Columns(otherEntity_id,parent_id);
使用xml或fluent并不重要。
<编辑:
没有引用映射没有错误,引用映射下面的错误
(我有这个错误几次,每当我有更多的值比映射列):
System.ArgumentOutOfRangeException:System.ArgumentOutOfRangeException:Der Indexaußerhalbdes Bereichs。 Er muss nicht negativ und kleiner als die Auflistung sein。
Parametername:index
bei System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument,ExceptionResource resource)
bei System.ThrowHelper.ThrowArgumentOutOfRangeException()
bei System.Collections.Generic.List`1 .get_Item(Int32索引)
bei Npgsql.NpgsqlParameterCollection.get_Item(Int32索引)
bei Npgsql.NpgsqlParameterCollection.GetParameter(Int32索引)
bei System.Data.Common.DbParameterCollection.System.Collections .IList.get_Item(Int32 index)
bei NHibernate.Type.Int16Type.Set(IDbCommand rs,Object value,Int32 index)
bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd,Object value,Int32 index)
bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand st,Object value,Int32 index,ISessionImplementor session)
bei NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st,Object value,Int32 begin,ISessionImplementor会话)
bei NHibernate.Persister.Entity.AbstractEntit yPersister.Dehydrate(Object id,Object [] fields,Object rowId,Boolean [] includeProperty,Boolean [] [] includeColumns,Int32表,IDbCommand语句,ISessionImplementor会话,Int32索引)
bei NHibernate.Persister.Entity。 AbstractEntityPersister.Insert(Object id,Object [] fields,Boolean [] notNull,Int32 j,SqlCommandInfo sql,Object obj,ISessionImplementor session)
bei NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,Object []字段,对象obj,ISessionImplementor会话)
bei NHibernate.Action.EntityInsertAction.Execute()
bei NHibernate.Engine.ActionQueue.Execute(IExecutable可执行文件)
bei NHibernate.Engine.ActionQueue.ExecuteActions (IList list)
bei NHibernate.Engine.ActionQueue.ExecuteActions()
bei NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
bei NHibernate.Event.Default.DefaultFlushEventListener.OnFlush (FlushEvent事件)
bei NHiberna te.Impl.SessionImpl.Flush()
测试代码
var e1 =新的实体{Id = 2,Other = other};
var e2 = new实体{Id = 3,Other = other,Parent = e1};
Session.Save(e1);
Session.Save(e2);
Session.Flush(); //在这里抛出
Session.Clear();
var key = new实体{Id = e2.Id,Other = e2.Other};
var loaded = Session.Get< Entity>(key);
编辑:
可以请别人告诉我吗?
解决方案
我仍然没有找到解决方案,并将映射更改为我的模型:
public class实体
{
public OtherEntity Other {get;组; }
//简单的int来区分同一个OtherEntity的不同实体
public int Id {get;组; }
public int ParentId {get;组; }
}
I have a tablestructure like this:
Table entity
(
otherEntity_id int // primarykey
id int // primarykey
parent_id int
)
and class
public class Entity
{
public OtherEntity Other { get; set; }
// simple int to discriminate different Entity for the same OtherEntity
public int Id { get; set; }
public Entity Parent { get; set; }
}
is it possible to map this to the class Entity? (if yes how)
the following throws on save, because there are not enough columns in the dbcommand, one is used twice:
CompositeId()
.KeyReference(e => e.Other, "otherEntity_id")
.KeyProperty(e => e.Id, "id")
References(e => e.Parent).Columns("otherEntity_id", "parent_id");
It doesnt matter using xml or fluent.
Edit: without the reference mapped no error, with reference mapped following error (i had this error several times, everytime when i have more values than mapped columns):
System.ArgumentOutOfRangeException: System.ArgumentOutOfRangeException : Der Index lag außerhalb des Bereichs. Er muss nicht negativ und kleiner als die Auflistung sein.
Parametername: index
bei System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
bei System.ThrowHelper.ThrowArgumentOutOfRangeException()
bei System.Collections.Generic.List`1.get_Item(Int32 index)
bei Npgsql.NpgsqlParameterCollection.get_Item(Int32 index)
bei Npgsql.NpgsqlParameterCollection.GetParameter(Int32 index)
bei System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
bei NHibernate.Type.Int16Type.Set(IDbCommand rs, Object value, Int32 index)
bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
bei NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object value, Int32 begin, ISessionImplementor session)
bei NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
bei NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
bei NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
bei NHibernate.Action.EntityInsertAction.Execute()
bei NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
bei NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
bei NHibernate.Engine.ActionQueue.ExecuteActions()
bei NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
bei NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
bei NHibernate.Impl.SessionImpl.Flush()
testcode
var e1 = new Entity { Id = 2, Other = other };
var e2 = new Entity { Id = 3, Other = other, Parent = e1 };
Session.Save(e1);
Session.Save(e2);
Session.Flush(); // throws here
Session.Clear();
var key = new Entity { Id = e2.Id, Other = e2.Other };
var loaded = Session.Get<Entity>(key);
Edit:
if it's not possible could please someone tell me?
I still haven't found a solution and mapped changed my model to:
public class Entity
{
public OtherEntity Other { get; set; }
// simple int to discriminate different Entity for the same OtherEntity
public int Id { get; set; }
public int ParentId { get; set; }
}
这篇关于nhibernate:在ManyToOne属性中使用compositeId的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!