使用 GUID 的 Sharp 架构

发布于 2024-10-09 00:52:58 字数 6052 浏览 1 评论 0原文

我正在使用 Sharp Architecture 1.6 VS2010 并使用 GUID 进行调查 但我遇到了一个无法解决的错误。

Person Entity

using System;
using NHibernate.Validator.Constraints;
using SharpArch.Core.DomainModel;

namespace SharpDemo.Core
{
   public class Person : EntityWithTypedId<Guid>
   {
       [NotNullNotEmpty(Message = "First name must be provided.")]
       public virtual string FirstName { get; set; }

       [NotNullNotEmpty(Message = "Last name must be provided.")]
       public virtual string LastName { get; set; }
   }
}

Fluent Mapping

using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using SharpDemo.Core;

namespace SharpDemo.Data.NHibernateMaps
{
   public class PersonMap : IAutoMappingOverride<Person>
   {
       public void Override(AutoMapping<Person> mapping)
       {
           mapping.Schema("dbo");
           mapping.Table("People");

           mapping.Id(x => x.Id).GeneratedBy.GuidComb();
       }
   }
}

解决方案成功构建,但在执行时遇到以下错误 调试(我一直无法解决)。任何帮助都是 赞赏。

源错误

Line 83:         private void InitializeNHibernateSession()
Line 84:         {
Line 85:             NHibernateSession.Init(
Line 86:                 webSessionStorage,
Line 87:                 new string[] { Server.MapPath("~/bin/SharpDemo.Data.dll") },

堆栈跟踪

[FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).]
   System.Guid..ctor(String g) +2486
   NHibernate.Type.GuidType.FromStringValue(String xml) +75
   NHibernate.Type.GuidType.StringToObject(String xml) +86
   NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo constructor) +644

[MappingException: Could not parse identifier unsaved-value: 0]
   NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo constructor) +872
   NHibernate.Tuple.PropertyFactory.BuildIdentifierProperty(PersistentClass mappedEntity, IIdentifierGenerator generator) +220
   NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory) +696
   NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory) +784
   NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) +379
   NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg) +182
   NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) +2117
   NHibernate.Cfg.Configuration.BuildSessionFactory() +189
   FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() +76

[FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  * Database was not configured through Database method.
]
   FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() +120
   SharpArch.Data.NHibernate.NHibernateSession.CreateSessionFactoryFor(String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, Configuration cfg, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:328
   SharpArch.Data.NHibernate.NHibernateSession.AddConfiguration(String factoryKey, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, Configuration cfg, String validatorCfgFile, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:138
   SharpArch.Data.NHibernate.NHibernateSession.AddConfiguration(String factoryKey, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, String cfgFile, IDictionary`2 cfgProperties, String validatorCfgFile, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:126
   SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage storage, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, String cfgFile, IDictionary`2 cfgProperties, String validatorCfgFile, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:101
   SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage storage, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, String cfgFile) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:51
   SharpDemo.Web.MvcApplication.InitializeNHibernateSession() in D:\Web\Mvc\Projects\Temp\SharpDemo\app\SharpDemo.Web\Global.asax.cs:85
   SharpDemo.Web.MvcApplication.<Application_BeginRequest>b__3() in D:\Web\Mvc\Projects\Temp\SharpDemo\app\SharpDemo.Web\Global.asax.cs:76
   SharpArch.Data.NHibernate.NHibernateInitializer.InitializeNHibernateOnce(Action initMethod) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateInitializer.cs:46
   SharpDemo.Web.MvcApplication.Application_BeginRequest(Object sender, EventArgs e) in D:\Web\Mvc\Projects\Temp\SharpDemo\app\SharpDemo.Web\Global.asax.cs:75
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

I'm using Sharp Architecture 1.6 VS2010 and investigating using GUIDs
but I'm encountering an error that I've been unable to resolve.

Person Entity

using System;
using NHibernate.Validator.Constraints;
using SharpArch.Core.DomainModel;

namespace SharpDemo.Core
{
   public class Person : EntityWithTypedId<Guid>
   {
       [NotNullNotEmpty(Message = "First name must be provided.")]
       public virtual string FirstName { get; set; }

       [NotNullNotEmpty(Message = "Last name must be provided.")]
       public virtual string LastName { get; set; }
   }
}

Fluent Mapping

using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using SharpDemo.Core;

namespace SharpDemo.Data.NHibernateMaps
{
   public class PersonMap : IAutoMappingOverride<Person>
   {
       public void Override(AutoMapping<Person> mapping)
       {
           mapping.Schema("dbo");
           mapping.Table("People");

           mapping.Id(x => x.Id).GeneratedBy.GuidComb();
       }
   }
}

The solution successfully builds but encounters the below error when
debugging (which I have been unable to resolve). Any assistance is
appreciated.

Source Error

Line 83:         private void InitializeNHibernateSession()
Line 84:         {
Line 85:             NHibernateSession.Init(
Line 86:                 webSessionStorage,
Line 87:                 new string[] { Server.MapPath("~/bin/SharpDemo.Data.dll") },

Stack Trace

[FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).]
   System.Guid..ctor(String g) +2486
   NHibernate.Type.GuidType.FromStringValue(String xml) +75
   NHibernate.Type.GuidType.StringToObject(String xml) +86
   NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo constructor) +644

[MappingException: Could not parse identifier unsaved-value: 0]
   NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo constructor) +872
   NHibernate.Tuple.PropertyFactory.BuildIdentifierProperty(PersistentClass mappedEntity, IIdentifierGenerator generator) +220
   NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory) +696
   NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory) +784
   NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) +379
   NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg) +182
   NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) +2117
   NHibernate.Cfg.Configuration.BuildSessionFactory() +189
   FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() +76

[FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  * Database was not configured through Database method.
]
   FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() +120
   SharpArch.Data.NHibernate.NHibernateSession.CreateSessionFactoryFor(String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, Configuration cfg, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:328
   SharpArch.Data.NHibernate.NHibernateSession.AddConfiguration(String factoryKey, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, Configuration cfg, String validatorCfgFile, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:138
   SharpArch.Data.NHibernate.NHibernateSession.AddConfiguration(String factoryKey, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, String cfgFile, IDictionary`2 cfgProperties, String validatorCfgFile, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:126
   SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage storage, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, String cfgFile, IDictionary`2 cfgProperties, String validatorCfgFile, IPersistenceConfigurer persistenceConfigurer) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:101
   SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage storage, String[] mappingAssemblies, AutoPersistenceModel autoPersistenceModel, String cfgFile) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs:51
   SharpDemo.Web.MvcApplication.InitializeNHibernateSession() in D:\Web\Mvc\Projects\Temp\SharpDemo\app\SharpDemo.Web\Global.asax.cs:85
   SharpDemo.Web.MvcApplication.<Application_BeginRequest>b__3() in D:\Web\Mvc\Projects\Temp\SharpDemo\app\SharpDemo.Web\Global.asax.cs:76
   SharpArch.Data.NHibernate.NHibernateInitializer.InitializeNHibernateOnce(Action initMethod) in e:\WorkSpaces\Git\SharpArchitecture\Trunk\src\SharpArch\SharpArch.Data\NHibernate\NHibernateInitializer.cs:46
   SharpDemo.Web.MvcApplication.Application_BeginRequest(Object sender, EventArgs e) in D:\Web\Mvc\Projects\Temp\SharpDemo\app\SharpDemo.Web\Global.asax.cs:75
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2024-10-16 00:52:58

看起来未保存的值被自动映射设置为 0。尝试将映射覆盖更改为:

mapping.Id(x => x.Id).GenerateBy.GuidComb().Default(Guid.Empty);

mapping.Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);

It looks like the unsaved value is being set to 0 by the automapping. Try changing your mapping override to:

mapping.Id(x => x.Id).GeneratedBy.GuidComb().Default(Guid.Empty);

mapping.Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文