gpt4 book ai didi

nhibernate - 参数超出范围异常 : Index was out of range

转载 作者:行者123 更新时间:2023-12-02 12:42:26 30 4
gpt4 key购买 nike

每当我使用PersitenceSpecification 类用于验证具有对值对象的引用。

    public class CatalogItem : DomainEntity
{
internal virtual Manufacturer Manufacturer { get; private
set; }
internal virtual String Name { get; private set; }

protected CatalogItem()
{}

public CatalogItem(String name, String manufacturer)
{
Name = name;
Manufacturer = new Manufacturer(manufacturer);
}
}

public class CatalogItemMapping : ClassMap<CatalogItem>
{
public CatalogItemMapping()
{
Id(catalogItem => catalogItem.Id);

Component<Manufacturer>(category => category.Manufacturer,
m => m.Map(manufacturer =>
manufacturer.Name));

Map(catalogItem => catalogItem.Name);
Map(Reveal.Property<CatalogItem>("Price"));
}
}

[TestFixture]
public class When_verifying_the_class_mapping_of_a_catalog_item
: NHibernateSpecification
{
[Test]
public void Then_a_catalog_object_should_be_persistable()
{
new PersistenceSpecification<CatalogItem>(Session)
.VerifyTheMappings();
}
}

[TestFixture]
public class NHibernateSpecification
: Specification
{
protected ISession Session { get; private set; }

protected override void Establish_context()
{
var configuration = new SQLiteConfiguration()
.InMemory()
.ShowSql()
.ToProperties();

var sessionSource = new SessionSource(configuration, new
RetailerPersistenceModel());
Session = sessionSource.CreateSession();

sessionSource.BuildSchema(Session);
ProvideInitialData(Session);

Session.Flush();
Session.Clear();
}

protected override void Dispose_context()
{
Session.Dispose();
Session = null;
}

protected virtual void ProvideInitialData(ISession session)
{}
}

这是我收到的错误:

TestCase 'Then_a_catalog_object_should_be_persistable' not executed: System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRangeException (ExceptionArgument argument, ExceptionResource resource) at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List1.get_Item(Int32
index)
at System.Data.SQLite.SQLiteParameterCollection.GetParameter(Int32
index)
at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item
(Int32 index)
at NHibernate.Type.GuidType.Set(IDbCommand
cmd, Object value, Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand
cmd, Object value, Int32 index)
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand
st, Object value, Int32 index,
ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate
(Object id, Object[] fields, Object
rowId, Boolean[] includeProperty,
Boolean[][] includeColumns, Int32
table, IDbCommand statement,
ISessionImplementor session, Int32
index)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object
id, Object[] fields, Boolean[]
notNull, Int32 j, SqlCommandInfo sql,
Object obj, ISessionImplementor
session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object
id, Object[] fields, Object obj,
ISessionImplementor session)
at NHibernate.Action.EntityInsertAction.Execute()
at NHibernate.Engine.ActionQueue.Execute(IExecutable
executable)
at NHibernate.Engine.ActionQueue.ExecuteActions(IList
list)
at NHibernate.Engine.ActionQueue.ExecuteActions()
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions
(IEventSource session)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush
(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
d:\Builds\FluentNH\src\FluentNHibernate\Testing
\PersistenceSpecification.cs(127,0):
at
FluentNHibernate.Testing.PersistenceSpecification
1.TransactionalSave (Object propertyValue) d:\Builds\FluentNH\src\FluentNHibernate\Testing \PersistenceSpecification.cs(105,0): at FluentNHibernate.Testing.PersistenceSpecification`1.VerifyTheMappings () C:\Source\SupplyChain\Tests\Retailer.IntegrationTests\Mappings \CatalogItemMappingSpecifications.cs(14,0): at SupplyChain.Retailer.IntegrationTests.Mappings.When_verifying_the_class_mapping_of_a_catalog_item.Then_a_catalog_object_should_be_persistable ()

很抱歉这篇文章很长,但这让我忙了好几天现在几个小时。这可能不是 FNH 造成的,因为我发现了这张 JIRA 票证NH 本身提到了类似的东西:

http://forum.hibernate.org/viewtopic.php?p=2395409

我仍然希望我的代码中做错了什么:-)。任何想到什么?

提前致谢

最佳答案

我自己找到了这个问题的解决方案首先是愚蠢。一旦我明白了,一切就都清楚了从流畅的 NH 映射生成 hbm 文件。

<class name="CatalogItem" table="`CatalogItem`" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version">
...

<property name="Name" length="100" type="String">
<column name="Name" />
</property>

...

<component name="Manufacturer" insert="false" update="true">
<property name="Name" length="100" type="String">
<column name="Name" />
</property>
</component>
</class>

请注意,Name 属性的列和制造商组件都映射到同一列。这就是为什么这导致了 ArgumentOutOfRangeException,因为有参数多于列名。我解决了这个问题明确指定组件映射的列名称:

组件(catalogItem => CatalogItem.Manufacturer, m => m.Map(制造商 => 制造商.名称,“制造商”));

又一个教训。

关于nhibernate - 参数超出范围异常 : Index was out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/600308/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com