Bar*_*zKP 5 c# nhibernate fluent-nhibernate fluent-nhibernate-mapping .net-4.6
我有以下枚举类型:
public enum EnumType
{
E1,
E2
}
Run Code Online (Sandbox Code Playgroud)
用于以下类:
public class X
{
public virtual int? Id { get; set; }
public virtual EnumType EnumProperty { get; set; }
public virtual string S { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想使用NHibernate在数据库中保留此类实例.为避免编写样板代码,我试图使用自动映射功能,如下所示:
private ISessionFactory CreateSessionFactory()
{
var mappings = AutoMap
.AssemblyOf<Domain.X>(new MyAutoMappingConfiguration());
this.NHibernateConfiguration = Fluently
.Configure()
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2012.ConnectionString(
b => b.FromConnectionStringWithKey("x")))
.Mappings(m => m.AutoMappings.Add(mappings))
.BuildConfiguration();
return this.NHibernateConfiguration.BuildSessionFactory();
}
Run Code Online (Sandbox Code Playgroud)
这里MyAutoMappingConfiguration看起来是这样的:
public class MyAutoMappingConfiguration: FluentNHibernate.Automapping.DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.Namespace == "Domain";
}
public override bool IsComponent(Type type)
{
return type.Name == "EnumType";
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用从此配置生成的模式来创建数据库时:
new SchemaExport(this.sessionProvider.NHibernateConfiguration)
.Execute(true, true, false);
Run Code Online (Sandbox Code Playgroud)
正在生成并执行以下脚本:
if exists (select * from dbo.sysobjects where id = object_id(N'[X]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [X]
create table [X] (
Id INT not null,
S NVARCHAR(255) null,
primary key (Id)
)
Run Code Online (Sandbox Code Playgroud)
为什么财产EnumProperty被忽略了?
当我X为这样的自动映射添加显式映射,或者(看似等效的)覆盖:
var mappings = AutoMap
.AssemblyOf<Domain.X>(new MyAutoMappingConfiguration())
.Override<Domain.X>(m =>
{
m.Table("X");
m.Id(x => x.Id);
m.Map(x => x.EnumProperty); // this works
m.Map(x => x.S);
});
Run Code Online (Sandbox Code Playgroud)
脚本生成正确:
if exists (select * from dbo.sysobjects where id = object_id(N'[X]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [X]
create table [X] (
Id INT not null,
EnumProperty NVARCHAR(255) null,
S NVARCHAR(255) null,
primary key (Id)
)
Run Code Online (Sandbox Code Playgroud)
这表明NHibernate正确映射所呈现的枚举的能力似乎没有任何问题.为什么无法自动映射应对此问题?
当我将以下方法添加到MyAutoMappingConfiguration:
public override bool ShouldMap(Member member)
{
var result = base.ShouldMap(member);
return result;
}
Run Code Online (Sandbox Code Playgroud)
并把断点时,result就是true为EnumProperty会员,这在某种程度上被忽略后面.
根据我的经验,枚举是开箱即用的,不需要做任何额外的事情(没有自定义类型或组件)。
所以,有两个问题:
IsComponent不应表明所讨论的枚举是一个组件:
public override bool IsComponent(Type type)
{
return base.IsComponent(type);
}
Run Code Online (Sandbox Code Playgroud)
(或者干脆删除实现)
ShouldMap不应表明枚举需要显式映射。无论如何它都会被映射,因为它是一个属性。因此,例如:
public override bool ShouldMap(Member member)
{
return base.ShouldMap(member) && member.CanWrite &&
!member.MemberInfo.IsDefined(typeof(NotMappedAttribute), false);
}
Run Code Online (Sandbox Code Playgroud)
在您的情况下,如果枚举位于同一名称空间中,您应该执行以下操作:
public override bool ShouldMap(Type type)
{
return type.Namespace == "Domain"
&& !type.IsEnum;
}
Run Code Online (Sandbox Code Playgroud)
(这有点违反直觉)
| 归档时间: |
|
| 查看次数: |
647 次 |
| 最近记录: |