来自表X的关联指向一个未映射的类:System.Guid

本文关键字:一个 映射 System Guid 关联 | 更新日期: 2023-09-27 18:15:40

我有这样一个类:

public class AttachmentLocation
{
    #region properties
    public virtual Guid UserId { get; set; }
    public virtual Guid LocationId { get; set; }
    public virtual long IndexInLocation { get; set; }
    #endregion
    #region contained foreign objects
    public virtual Attachment Attachment { get; set; }
    #endregion

    #region Methods need to override for CompositeId
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as AttachmentLocation;
        if (t == null)
            return false;
        if (UserId == t.UserId && LocationId == t.LocationId && Attachment.Id == t.Attachment.Id)
            return true;
        return false;
    }
    public override int GetHashCode()
    {
        return (UserId + "|" + LocationId + "|" + Attachment.Id).GetHashCode();
    }
    #endregion
}

我有这个Fluent Mapping:

public class AttachmentLocaionMap : ClassMap<AttachmentLocation>
{
    public AttachmentLocaionMap()
    {
        Table("Metadata_AttachmentLocation");
        CompositeId()
            .KeyReference(x => x.UserId, "UserId")
            .KeyReference(x => x.Attachment.Id, "AttachmentId")
            .KeyReference(x => x.LocationId, "LocationId");
        Map(x => x.IndexInLocation).Not.Nullable();
        Map(x => x.LocationId).Not.Nullable();
        HasOne(x => x.Attachment);
    }
}

,我注册:

SessionFactory = Fluently.Configure(configuration).Mappings(m =>
{
    m.FluentMappings.AddFromAssemblyOf<AttachmentLocaionMap>();           
    m.FluentMappings.AddFromAssemblyOf<FriendDetailsMap>();
}).BuildSessionFactory();

我得到这个运行时错误:

Metadata_AttachmentLocation表中的关联指向一个未映射类:System。Guid

描述:在执行过程中发生未处理的异常当前的web请求。请查看堆栈跟踪了解更多信息有关错误及其在代码中的起源的信息。

Exception Details: NHibernate。MappingException:来自的关联表Metadata_AttachmentLocation引用一个未映射的类:系统。Guid

源错误:

第19行:第20行:第21行:SessionFactory =Fluently.Configure(配置)。映射(m =>第22行:{第23行:

m.FluentMappings.AddFromAssemblyOf ();

源文件:C:'Users'elad'Documents'Visual Studio2010 ' ' SVN ' UGI ' UGI '基础设施项目' ' NHibernateAdapter ' NHibernateHelper.cs Dal '适配器第21行

堆栈跟踪:

[MappingException:来自表的关联Metadata_AttachmentLocation引用一个未映射的类:System。Guid)NHibernate.Cfg.Configuration。LogAndThrow(异常异常)d: ' CSharp ' NH ' NH ' nhibernate ' src ' nhibernate ' Cfg ' Configuration.cs: 342
NHibernate.Cfg.Configuration。SecondPassCompileForeignKeys(表表,我已经做完了d: ' CSharp ' NH ' NH ' nhibernate ' src ' nhibernate ' Cfg ' Configuration.cs: 1169
NHibernate.Cfg.Configuration.SecondPassCompile ()d: ' CSharp ' NH ' NH ' nhibernate ' src ' nhibernate ' Cfg ' Configuration.cs: 1120
NHibernate.Cfg.Configuration.BuildSessionFactory ()d: ' CSharp ' NH ' NH ' nhibernate ' src ' nhibernate ' Cfg ' Configuration.cs: 1249
FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory ()构建d: ' ' FluentNH-v1.x-nh3 ' src ' FluentNHibernate ' Cfg ' FluentConfiguration.cs: 227

[FluentConfigurationException:一个无效或不完整的配置。在创建SessionFactory时使用。检查PotentialReasons

  • 没有通过数据库方法配置数据库。]
    FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory ()构建d: ' ' FluentNH-v1.x-nh3 ' src ' FluentNHibernate ' Cfg ' FluentConfiguration.cs: 232Ugi.Infrastructure.Dal.Adapters.NHibernateAdapter.NHibernateHelper.OpenSession ()C:'Users'elad'Documents'Visual Studio2010 ' '项目SVN ' UGI ' UGI ' NHibernateAdapter ' '基础设施' Dal '适配器NHibernateHelper.cs: 21Ugi.Infrastructure.Dal.Adapters.NHibernateAdapter.NHibernateHelper.GetSession ()C:'Users'elad'Documents'Visual Studio2010 ' '项目SVN ' UGI ' UGI ' NHibernateAdapter ' '基础设施' Dal '适配器NHibernateHelper.cs: 36Ugi.Infrastructure.Dal.Adapters.NHibernateAdapter.NHibernateDal . 1..ctor() in C:'Users'elad'Documents'Visual Studio 2010'Projects'SVN'UGI'Ugi'Infrastructure'Dal'Adapters'NHibernateAdapter'NHibernateDal.cs:16 Ugi.Infrastructure.Dal.Adapters.NHibernateAdapter.NHibernateDalFactory.GetDal() in C:'Users'elad'Documents'Visual Studio 2010'Projects'SVN'UGI'Ugi'Infrastructure'Dal'Adapters'NHibernateAdapter'NHibernateDalFactory.cs:17 Ugi.Server.Sources.Logic.SourcesService..ctor() in C:'Users'elad'Documents'Visual Studio 2010'Projects'SVN'UGI'Ugi'Server'Sources'Logic'SourcesService.cs:36
    BuildUp_Ugi.Server.Sources.Logic.SourcesService(IBuilderContext ) +153 Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) in c:'EntLib'UnityTemp'Compile'Unity'Unity'Src'ObjectBuilder'Strategies'BuildPlan'DynamicMethod'DynamicMethodBuildPlan.cs:37 Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in c:'EntLib'UnityTemp'Compile'Unity'Unity'Src'ObjectBuilder'Strategies'BuildPlan'BuildPlanStrategy.cs:43 Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in c:'EntLib'UnityTemp'Compile'Unity'Unity'Src'ObjectBuilder'Strategies'StrategyChain.cs:110 Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable
    . resolverOverrides) inc: ' EntLib ' UnityTemp '编译' ' ' Src ' UnityContainer.cs统一:统一512

[ResolutionFailedException:解析依赖项失败,类型。= " Ugi.Common.Model.Sources。ISourcesService", name = "(none)"。调用构造函数时发生异常Ugi.Server.Sources.Logic.SourcesService()。异常:FluentConfigurationException—无效或不完整的配置在创建SessionFactory时使用。检查PotentialReasons

  • 没有通过数据库方法配置数据库。

我怎样才能解决这个问题?

TIA

来自表X的关联指向一个未映射的类:System.Guid

如果您想在组合键中映射简单属性(这不是与ORM相关的"标准方式",但在某些情况下它可以帮助您),您可以使用KeyProperty而不是KeyReference以这种方式映射键:

CompositeId()
    .KeyProperty(x => x.UserId, "UserId")
    .KeyProperty(x => x.AttachmentId, "AttachmentId")
    .KeyProperty(x => x.LocationId, "LocationId");

这些是错误的:

public virtual Guid UserId { get; set; }
public virtual Guid LocationId { get; set; }

CompositeId()
    .KeyReference(x => x.UserId, "UserId")
    .KeyReference(x => x.Attachment.Id, "AttachmentId")
    .KeyReference(x => x.LocationId, "LocationId");

你应该有适当的引用:

public virtual User User { get; set; }
public virtual Location Location { get; set; }

和映射:

CompositeId()
    .KeyReference(x => x.User, "UserId")
    .KeyReference(x => x.Attachment, "AttachmentId")
    .KeyReference(x => x.Location, "LocationId");

您的Attachmentmodel是否正确引用AttachmentLocation ?如果没有,您可能需要尝试References()而不是HasOne

我的流畅配置是这样的:

Fluently.Configure().Database(configuration)

你有什么

Fluently.Configure(configuration)

我不确定什么应该是不同的过载配置,但考虑到关于不使用数据库方法的消息,我想这将为您修复它。