如何使用Fluent NHibernate的类映射映射IDictionary <string,string =“”>,并测试该映射?</string,>

时间:2012-05-01 12:25:00

标签: nhibernate fluent-nhibernate masstransit saga

我的MassTransit传奇只有一个无法正确映射的字段。它是 -

    public virtual IDictionary<string, string> Rows
    {
        get { return _rows; }
        set { _rows = value; }
    }

我尝试过几次映射 -

    publich ProjCTSagaMap() // ctor for my ClassMap subclass
    {
        /*HasMany(x => x.Rows)
            .AsMap("ProjectNumber")
            .KeyColumns.Add("Row", mapping => mapping.Name("Row").SqlType("nvarchar").Not.Nullable())
            .Cascade.AllDeleteOrphan()
            .Table("ProjCtSagaRow");*/

        HasMany(x => x.Rows)
            .Table("ProjCtSagaRow")
            .KeyColumn("CorrelationId")
            .Element("Row")
            .AsMap("ProjectNumber")
            .Inverse();
    }

我已经编写了一个单元测试来测试映射 -

    [Test] public void ProjCtSagaMapTest()
    {
        using (var session = Utility.CreateSession<ProjCtSaga, ProjCtSagaMap>())
        {
            new PersistenceSpecification<ProjCtSaga>(session)
                .CheckReference(
                    i => i.Rows,
                    new Dictionary<string, string> { { "key", "value" } },
                    new DictionaryComparer<string, string>())
                .VerifyTheMappings();
        }

我不确定问题的实际位置,映射或测试中。单元测试中的实际错误是 -

NHibernate.MappingException occurred
  Message=No persister for: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
  Source=NHibernate
  StackTrace:
       at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
       at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
       at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
       at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
       at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
       at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
       at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
       at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
       at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
       at NHibernate.Impl.SessionImpl.Save(Object obj)
       at FluentNHibernate.Testing.PersistenceSpecification`1.TransactionalSave(Object propertyValue) in d:\Builds\FluentNH\src\FluentNHibernate\Testing\PersistenceSpecification.cs:line 79
  InnerException: 

有人可以解释如何使用ClassMap映射IDictionary并使用PersistenceSpecification测试该映射吗... VerifyMappings()?

PS - FluentNHibernate的版本是1.3.0.717,NHibernate版本是3.2.0.4000。我确实必须在本地编译它以进行一次轻微且无关的更改。

谢谢!


这是我现在得到的其余错误 -

System.FormatException : Failed to convert parameter value from a String to a Int32.
  ----> System.FormatException : Input string was not in a correct format.
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
at System.Data.SqlClient.SqlParameter.GetCoercedValue()
at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters)
at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, ref _SqlRPC rpc)
at System.Data.SqlClient.SqlCommand.AddBatchCommand(String commandText, SqlParameterCollection parameters, CommandType cmdType)
at System.Data.SqlClient.SqlCommandSet.ExecuteNonQuery()
at NHibernate.AdoNet.SqlClientBatchingBatcher.DoExecuteBatch(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatchWithTiming(IDbCommand ps)
at NHibernate.AdoNet.AbstractBatcher.ExecuteBatch()
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()
at FluentNHibernate.Testing.PersistenceSpecification`1.TransactionalSave(Object propertyValue) in d:\Builds\FluentNH\src\FluentNHibernate\Testing\PersistenceSpecification.cs: line 80
at FluentNHibernate.Testing.PersistenceSpecification`1.VerifyTheMappings(T first) in d:\Builds\FluentNH\src\FluentNHibernate\Testing\PersistenceSpecification.cs: line 52
at Appalappa.Tests.ProjCtTests.ProjCtSagaMapTest() in ProjCtTests.cs: line 32
--FormatException
at System.Number.StringToNumber(String str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)

1 个答案:

答案 0 :(得分:2)

摆脱.Inverse();因为元素无法维持反向引用,所以它是一个字符串。

CheckReference仅供参考,但您有一个集合。使用CheckEnumerableCheckList

更新:这对我有用

.CheckComponentList(p => p.Rows, 
                    new[] { new KeyValuePair<string, string>("1", "2") }, 
                    (p, item) => p.Rows.Add(item));