非空字段的流畅 Nhibernate Automap 约定

发布于 2024-08-28 02:02:04 字数 454 浏览 12 评论 0原文

有人可以帮忙吗,我如何指示 automap 不为空 一个专栏?

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"/>

我知道可以使用 fluence-map 来完成。但我想知道 自动映射方式。

Could some one help, how would I instruct automap to have not-null for
a column?

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

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

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

But I am getting the following:

 <column name="SessionWeek"/>

I know it can be done using fluent-map. but i would like to know it in
auto-mapping way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情绪操控生活 2024-09-04 02:02:04

谢谢。另外,对于引用属性,需要完成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();

    }
}

Thank you. Also, for reference properties ReferenceConvention need to be done. This is the code that works:

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

    }
}
度的依靠╰つ 2024-09-04 02:02:04

这是我的做法,基本上取自您在代码中看到的链接。还有一些其他有用的约定以及

HTH,
贝里尔

/// <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();
    }
}

Here is the way I do it, basically taken from the link you see in the code. There are some other useful conventions there as well

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();
    }
}
给不了的爱 2024-09-04 02:02:04

如果您对自动映射结果非常满意,但偶尔需要覆盖它(例如类中的几个属性),我发现为该类实现 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>();

与 ClassMap 类似 - 但是您不需要描述课程中的每个字段。
这种方法与实体框架的 Code First Fluent API 方式非常相似。

If you are mostly happy with Automapping results but occasionally need to override it for say a couple of properties in a class I find implementing a IAutoMappingOverride for that class the easiest way to achieve that:

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

And then use them like this:

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

Similar to ClassMaps - but you don't need to describe every field in the class.
This approach is very similar to the Entity Framework's Code First Fluent API way.

我喜欢麦丽素 2024-09-04 02:02:04
public class Paper Map : IAutoMappingOverride<Paper >
{
    public void Override(AutoMapping<Paper> mapping)
    {
        mapping.Map(x => x.ReferenceNumber).Not.Nullable();
    }
}

默认情况下,Int32 不是可为 null 的类型。 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;
    }
}

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

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

Int32 is not nullable type by default. Int32? is nullable, so you make it non-nullable just by specifying it as Int32.

You can use conventions to do this automatically. I am not sure which convention to use, but have a look at FluentNHibernate.Conventions.Instances to find the right one. It'll look like this.

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;
    }
}

Just add this convention to your automapping.

可是我不能没有你 2024-09-04 02:02:04

我经常发现我的列不为空,因此我更喜欢制定此约定并仅将列指定为可为空:

  /// <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));
    }
  }

I find more often than not, my columns are not null, so I prefer make this convention and only specify columns as nullable:

  /// <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));
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文