8

我正在設置一個新的解決方案來映射從Access數據庫導入的SQL數據。自動映射對我定義的實體工作正常,但我需要能夠使用Fluent映射類型來訪問.NotFound.Ignore擴展。流利的nHibernate:不支持的映射類型

我在另一個項目中使用完全相同的語法(我認爲!),它工作正常。我是否犯過小學生錯誤,導致「不支持的映射類型」IT.Classes.Entities.Admin'「?任何幫助深表感謝。

DDL:

CREATE TABLE [dbo].[Admin](
    [ID] [int] NOT NULL, 
    [primaryEmail] [nvarchar](50) NULL, 
    [secondaryEmail] [nvarchar](50) NULL, 
    [highPriorityEmail] [nvarchar](50) NULL, 
    [MobileEmail] [nvarchar](50) NULL, 
    [EmailUser] [bit] NOT NULL, 
    [HelpDeskMessage] [nvarchar](max) NULL 
) ON [PRIMARY] 

實體:

namespace IT.Classes.Entities 
{ 
    public class Admin 
    { 
    public virtual bool EmailUser { get; set; } 

    public virtual string HelpdeskMessage { get; set; } 

    public virtual string HighPriorityEmail { get; set; } 

    public virtual int Id { get; set; } 

    public virtual string MobileEmail { get; set; } 

    public virtual string PrimaryEmail { get; set; } 

    public virtual string SecondaryEmail { get; set; } 
    } 
} 

映射:使用FluentNHibernate.Mapping

;使用IT.Classes.Entities的 ;

namespace IT.Classes.Mappings 
{ 
    public sealed class AdminMap : ClassMap<Admin> 
    { 
     public AdminMap() 
     { 
      this.Id(x => x.Id); 
      this.Map(x => x.EmailUser); 
      this.Map(x => x.HelpdeskMessage); 
      this.Map(x => x.HighPriorityEmail); 
      this.Map(x => x.MobileEmail); 
      this.Map(x => x.PrimaryEmail); 
      this.Map(x => x.SecondaryEmail); 
     } 
    } 
} 

會話工廠:

private static ISessionFactory CreateItHelpdeskSessionFactory() 
{ 
    return 
     Fluently.Configure().Database(
      MsSqlConfiguration.MsSql2008.ConnectionString(
       ConfigurationManager.ConnectionStrings["ITHelpdesk"].ConnectionString)) 
       .Mappings(m => m.FluentMappings.Add<Admin>()) 
       .Mappings(m => m.FluentMappings.Add<Applications>()) 
       .Mappings(m => m.FluentMappings.Add<Category>()) 
       .Mappings(m => m.FluentMappings.Add<Log>()) 
       .Mappings(m => m.FluentMappings.Add<Multipliers>()) 
       .Mappings(m => m.FluentMappings.Add<Os>()) 
       .Mappings(m => m.FluentMappings.Add<Priority>()) 
       .Mappings(m => m.FluentMappings.Add<Request>()) 
       .Mappings(m => m.FluentMappings.Add<Status>()) 
       .BuildSessionFactory(); 
} 

編輯

我想我會出口自動映射,看看他們是否會突出我流利的映射問題,但他們似乎是正確的:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="IT.Classes.Entities.Admin, ITClasses, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Admin`"> 
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="Id" /> 
     <generator class="identity" /> 
    </id> 
    <property name="EmailUser" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="EmailUser" /> 
    </property> 
    <property name="HelpdeskMessage" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="HelpdeskMessage" /> 
    </property> 
    <property name="HighPriorityEmail" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="HighPriorityEmail" /> 
    </property> 
    <property name="MobileEmail" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="MobileEmail" /> 
    </property> 
    <property name="PrimaryEmail" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="PrimaryEmail" /> 
    </property> 
    <property name="SecondaryEmail" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <column name="SecondaryEmail" /> 
    </property> 
    </class> 
</hibernate-mapping> 

編輯2

完整的堆棧跟蹤。注意potentialreasons = 0

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

---> System.InvalidOperationException: Unsupported mapping type 'IT.Classes.Entities.Admin' 
    at FluentNHibernate.PersistenceModel.Add(Type type) in d:\Builds\FluentNH\src\FluentNHibernate\PersistenceModel.cs:line 152 
    at FluentNHibernate.Cfg.FluentMappingsContainer.Apply(PersistenceModel model) in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentMappingsContainer.cs:line 127 
    at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\MappingConfiguration.cs:line 84 
    at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 252 
    --- End of inner exception stack trace --- 
    at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 264 
    at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 230 
    --- End of inner exception stack trace --- 
    at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 235 
    at ConsoleChecker.Program.CreateItHelpdeskSessionFactory() in E:\code\code_testing\2010\ASPHelpdesks\ConsoleChecker\Program.cs:line 48 
    at ConsoleChecker.Program.GetRequests() in E:\code\code_testing\2010\ASPHelpdesks\ConsoleChecker\Program.cs:line 40 
    at ConsoleChecker.Program.Main(String[] args) in E:\code\code_testing\2010\ASPHelpdesks\ConsoleChecker\Program.cs:line 24 
+0

我有一個想法,認爲缺乏主鍵在表上可能會導致問題,所以我將Id列設置爲PK,但仍然是相同的錯誤。 – Rowan 2012-01-04 08:37:04

+0

進一步思考 - 創建一個空白數據庫並使用模式生成工具嘗試自動創建表並排除數據庫本身的任何問題。這仍然失敗,同樣的錯誤。 – Rowan 2012-01-04 10:45:05

+0

何時發生異常?請發佈完整的例外! Exception.ToString() – 2012-01-04 14:17:44

回答

9

在配置代碼,在流利映射中提到的類型應該是映射類,而不是類被映射。

也就是說,代替:

return 
    Fluently.Configure().Database(
     MsSqlConfiguration.MsSql2008.ConnectionString(
      ConfigurationManager.ConnectionStrings["ITHelpdesk"].ConnectionString)) 
      .Mappings(m => m.FluentMappings.Add<Admin>()) 
      .Mappings(m => m.FluentMappings.Add<Applications>()) 
      .Mappings(m => m.FluentMappings.Add<Category>()) 
      .Mappings(m => m.FluentMappings.Add<Log>()) 
      .Mappings(m => m.FluentMappings.Add<Multipliers>()) 
      .Mappings(m => m.FluentMappings.Add<Os>()) 
      .Mappings(m => m.FluentMappings.Add<Priority>()) 
      .Mappings(m => m.FluentMappings.Add<Request>()) 
      .Mappings(m => m.FluentMappings.Add<Status>()) 
      .BuildSessionFactory(); 

你需要說:

return 
    Fluently.Configure().Database(
     MsSqlConfiguration.MsSql2008.ConnectionString(
      ConfigurationManager.ConnectionStrings["ITHelpdesk"].ConnectionString)) 
      .Mappings(m => m.FluentMappings.Add<AdminMap>()) 
      .Mappings(m => m.FluentMappings.Add<ApplicationsMap>()) 
      .Mappings(m => m.FluentMappings.Add<CategoryMap>()) 
      .Mappings(m => m.FluentMappings.Add<LogMap>()) 
      .Mappings(m => m.FluentMappings.Add<MultipliersMap>()) 
      .Mappings(m => m.FluentMappings.Add<OsMap>()) 
      .Mappings(m => m.FluentMappings.Add<PriorityMap>()) 
      .Mappings(m => m.FluentMappings.Add<RequestMap>()) 
      .Mappings(m => m.FluentMappings.Add<StatusMap>()) 
      .BuildSessionFactory(); 

更妙的是,爲了避免爲每個類映射一個單獨的行,使用.AddFromAssemblyOf <>( )方法,該方法使用反射來添加給定程序集中的所有類圖:

return 
    Fluently.Configure().Database(
     MsSqlConfiguration.MsSql2008.ConnectionString(
      ConfigurationManager.ConnectionStrings["ITHelpdesk"].ConnectionString)) 
      .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AdminMap>()) 
      .BuildSessionFactory(); 
+0

謝謝,所以這是一個小學生的錯誤! – Rowan 2012-01-04 16:55:43

+1

更好的方法將繼承所有的地圖到一些接口,說IMap和使用.Mappings(m => m.FluentMappings.AddFromAssemblyOf ()) – 2013-08-12 21:19:44