Fluent nHibernate:不支持的映射类型
我正在设置一个新的解决方案来映射从 Access 数据库导入的 SQL 数据。自动映射对我定义的实体运行良好,但我需要能够使用 Fluent 映射类型来访问 .NotFound.Ignore 扩展。
我在另一个项目中使用了完全相同的语法(我认为!),效果很好。我是否犯了一个小学生错误导致“不支持的映射类型'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
完整堆栈跟踪。注意潜在原因 = 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
I'm setting up a new solution to map against SQL data that has been imported from an Access database. Auto Mappings work fine against the entities I have defined but I need to be able to use the Fluent mapping type to give access to the .NotFound.Ignore extension.
I have used exactly the same syntax in another project (I think!) which works fine. Have I made a schoolboy error to cause the "Unsupported mapping type 'IT.Classes.Entities.Admin'"? Any help is much appreciated.
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]
Entity:
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; }
}
}
Mapping:
using FluentNHibernate.Mapping;
using 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);
}
}
}
Session Factory:
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();
}
EDIT
I thought I would export the auto mappings to see if they would highlight a problem in my fluent mappings but they seem to be correct:
<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>
EDIT 2
Full stack trace. Note 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的配置代码中,Fluent Mappings 中提到的类型应该是映射类,而不是被映射的类。
也就是说,而不是:
您需要说:
更好的是,为了避免每个类映射都有单独的行,请使用 .AddFromAssemblyOf<>() 方法,该方法使用反射来添加给定程序集中的所有类映射:
In your configuration code, the types mentioned in the Fluent Mappings should be the map classes, not the classes being mapped.
That is, instead of:
You need to say:
Even better, to avoid having a separate line for every class map, use the .AddFromAssemblyOf<>() method, which uses reflection to add all class maps from a given assembly: