表X中的关联引用了未映射的类:System.Guid

时间:2011-11-26 00:55:32

标签: c# .net nhibernate fluent-nhibernate

我有这堂课:

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中的关联是指一个   unmapped类:System.Guid

     

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

     

异常详细信息:NHibernate.MappingException:来自的关联   表Metadata_AttachmentLocation是指未映射的类:   的System.Guid

     

来源错误:

     

第19行:第20行:第21行:SessionFactory =   Fluently.Configure(配置).Mappings(m =&gt;第22行:{   第23行:
  m.FluentMappings.AddFromAssemblyOf();

     

源文件:C:\ Users \ elad \ Documents \ Visual Studio   2010 \项目\ SVN \ UGI \卯木\基础设施\达尔\适配器\ NHibernateAdapter \ NHibernateHelper.cs   行: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(表格表,   ISet完成)   d:\ CSHARP \ NH \ NH \的nhibernate \ SRC \ NHibernate的\ CFG \ Configuration.cs:1169
  NHibernate.Cfg.Configuration.SecondPassCompile()in   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()in   d:\构建\ FluentNH-1.x版-NH 3 \ SRC \ FluentNHibernate \ CFG \ FluentConfiguration.cs:227

     

[FluentConfigurationException:配置无效或不完整   在创建SessionFactory时使用。检查PotentialReasons   集合和InnerException以获取更多细节。

     
      
  • 未通过数据库方法配置数据库。 ]
      FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()in   d:\构建\ FluentNH-1.x版-NH 3 \ SRC \ FluentNHibernate \ CFG \ FluentConfiguration.cs:232   Ugi.Infrastructure.Dal.Adapters.NHibernateAdapter.NHibernateHelper.OpenSession()   在C:\ Users \ elad \ Documents \ Visual Studio中   2010 \项目\ SVN \ UGI \卯木\基础设施\达尔\适配器\ NHibernateAdapter \ NHibernateHelper.cs:21   Ugi.Infrastructure.Dal.Adapters.NHibernateAdapter.NHibernateHelper.GetSession()   在C:\ Users \ elad \ Documents \ Visual Studio中   2010 \项目\ SVN \ UGI \卯木\基础设施\达尔\适配器\ NHibernateAdapter \ NHibernateHelper.cs:36   Ugi.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
    1 resolverOverrides)in   C:\ EntLib \ UnityTemp \编译\统一\统一\ SRC \ UnityContainer.cs:512
  •   
     

[ResolutionFailedException:依赖项的解析失败,输入   =“Ugi.Common.Model.Sources.ISourcesService”,name =“(none)”。发生异常时:调用构造函数   Ugi.Server.Sources.Logic.SourcesService()。例外情况是:   FluentConfigurationException - 配置无效或不完整   在创建SessionFactory时使用。检查PotentialReasons   集合和InnerException以获取更多细节。

     
      
  • 未通过数据库方法配置数据库。
  •   

我该如何解决?

TIA

4 个答案:

答案 0 :(得分:16)

如果要在复合键中映射简单属性(它不是与ORM关联的“标准方法”,但在某些情况下它可以帮助您),您可以使用KeyProperty而不是KeyReference来映射键方式:

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

答案 1 :(得分:4)

这些都是错的:

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");

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我的流畅配置看起来像这样:

Fluently.Configure().Database(configuration)

你有什么

Fluently.Configure(configuration)

我不确定配置的重载应该有什么不同,但是鉴于不使用Database方法的消息,我想这会为你解决它。