2010-04-09 60 views
7

有人可以帮助,我将如何指示自动映射非空为 列?流利Nhibernate自动映射非空字段约定

public class Paper : Entity 
{ 
    public Paper() { } 

      [DomainSignature] 
      [NotNull, NotEmpty] 
      public virtual string ReferenceNumber { get; set; } 

      [NotNull] 
      public virtual Int32 SessionWeek { get; set; } 
} 

但我得到以下几点:

<column name="SessionWeek"/> 

我知道它可以用流利的地图来完成。但我想知道它在 自动映射的方式。

回答

4

谢谢。此外,需要完成参考属性ReferenceConvention。这是工作的代码:

public class ColumnNullConvention : IPropertyConvention 
{ 
    public void Apply(IPropertyInstance instance) 
    { 
     if (instance.Property.MemberInfo.IsDefined(typeof(NotNullAttribute), false)) 
      instance.Not.Nullable(); 
    } 

} public class ReferenceConvention : IReferenceConvention 
{ 
    public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance) 
    { 
     instance.Column(instance.Property.Name + "Fk"); 


     if (instance.Property.MemberInfo.IsDefined(typeof(NotNullAttribute), false)) 
      instance.Not.Nullable(); 

    } 
} 
0
public class Paper Map : IAutoMappingOverride<Paper > 
{ 
    public void Override(AutoMapping<Paper> mapping) 
    { 
     mapping.Map(x => x.ReferenceNumber).Not.Nullable(); 
    } 
} 

默认情况下,Int32不是可以为空的类型。 INT32?是可以为空的,所以你只需将它指定为Int32即可使其不可空。

您可以使用约定自动执行此操作。我不确定使用哪种约定,但可以查看FluentNHibernate.Conventions.Instances来找到合适的约定。它看起来像这样。

public class ColumnConvention : IColumnConvention 
{ 
    public void Apply(FluentNHibernate.Conventions.Instances.ColumnInstance instance) 
    { 
     if (instance.EntityType.IsDefined(typeof(NotNullAttribute), false)) 
      instance.NotNull = true; 
    } 

    public void Apply(FluentNHibernate.Conventions.Instances.IColumnInstance instance) 
    { 
     return; 
    } 
} 

只需将此约定添加到您的自动映射中即可。

+0

谢谢,但是如果我在我的应用程序中的许多实体中有许多属性不为null,那该怎么办? 我会重写它们吗?尽管如此,这听起来并不正确。 – Robie 2010-04-09 09:59:07

+0

nHibernate验证器是一个单独的东西。您将不得不检查自动映射器中的属性。 – 2010-04-09 10:19:15

+0

非常感谢您的回复。好的。所以,它意味着没有AutoMapping约定来指示Fluent Nhibernate指定一个非空的字段,然后覆盖我的应用程序的所有属性。这让我感到AutoMap缺乏一个非常基本的功能。如果我需要将每个类映射作为alomost遍历,它们都具有或多或少的“非空”字段,那么为什么有人应该使用自动映射而不是流式映射。请分享你的想法。 – Robie 2010-04-09 11:00:28

1

这里是我做的方式,基本上是从你在代码中看到链接服用。还有一些其他有用的约定有作为

HTH,
Berryl

/// <summary> 
/// If nullability for the column has not been specified explicitly to allow NULL, then set to “NOT NULL”. 
/// </summary> 
/// <remarks>see http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/</remarks> 
public class ColumnNullabilityConvention : IPropertyConvention, IPropertyConventionAcceptance 
{ 
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) 
    { 
     criteria.Expect(x => x.Nullable, Is.Not.Set); 
    } 

    public void Apply(IPropertyInstance instance) 
    { 
     instance.Not.Nullable(); 
    } 
} 
+1

这对我最好的工作。非空引用,但是使用这种格式,只需更改IPropertyConvention和IPropertyConventionAcceptance以使用IReferenceConvention和IReferenceConventionAcceptance。 – 2011-06-08 15:51:30

0

我觉得往往不是,我的专栏是不为空,所以我更喜欢做这个惯例,只有指定为空列:

/// <summary> 
    /// Indicates that a column should allow nulls 
    /// </summary> 
    [Serializable] 
    [AttributeUsage(AttributeTargets.Property)] 
    public class NullableAttribute : Attribute 
    { 
    } 



public class ColumnIsNotNullByDefaultConvention : IPropertyConvention, IPropertyConventionAcceptance 
    { 
    public void Apply(IPropertyInstance instance) 
    { 
     instance.Not.Nullable(); 
    } 

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) 
    { 
     criteria.Expect(c => !c.Property.MemberInfo.IsDefined(typeof(NullableAttribute), false)); 
    } 
    } 
1

如果你多幸福与自动映射的结果,但偶尔也需要重写它说一个类中的一些属性,我觉得实现IAutoMappingOverride该类的最简单的方法实现这一目标:

public class UserMappingOverride : IAutoMappingOverride<User> 
{ 
     public void Override(AutoMapping<User> mapping) 
     { 
      mapping.Map(x => x.UserName).Column("User").Length(100).Not.Nullable(); 
     } 
} 

,然后用它们像这样:

AutoMap.AssemblyOf<User>().UseOverridesFromAssemblyOf<UserMappingOverride>(); 

类似ClassMaps - 但你并不需要描述在类中的每个领域。 这种方法与实体框架的Code First Fluent API方式非常相似。