流利NHibernate强制外键参考不可空 [英] Fluent NHibernate enforce Not Nullable on Foreign Key Reference

查看:103
本文介绍了流利NHibernate强制外键参考不可空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用一些流畅的NHibernate AutoMap约定让我的脚湿了,碰上了一些我无法想象的东西。我假设我只是没有在正确的地方寻找...
基本上试图在一对多的关系的多方面执行非空。
看起来,使用自动映射,它总是使数据库中的父属性Id为空。

我在StackOverFlow上做了一些搜索,发现了类似的问题,但没有任何关于AutoMapping和惯例,但(除非我错过了)。

快速示例...

<$ p $ ();} {this.Jobs = new List< Job>(); }
public virtual Guid Id {get;组; }
public virtual string Name {get;组; }
公共虚拟IList<作业>工作{get;保护组}
}

public class Job //有许多作业
{
public virtual Guid Id {get;组; }
public virtual string Name {get;组; }

//试图使这个字段在数据库中不可空。
public virtual Group Group {get;组; }
}

我以为我可以创建一个像.. 。

  public class OneToManyConvention:IHasOneConvention 
{
public void Apply(IOneToOneInstance instance)
{
// Nullable()不是一个有效的方法...
instance.Not.Nullable();




$ b但是看起来IOneToOnInstance没有Nullable ) 方法。我可以做到这一点,如果我创建一个地图文件作业,但试图避免任何地图文件,并坚持自动映射。

我碰到这个 Fluent group list链接描述类似的内容。



其中描述了这样的内容...

  public class NotNullPropertyConvention:IPropertyConvention 
{
public bool Accept(IProperty target)
{
return true;
}
public void Apply(IProperty target)
{
target.Not.Nullable();






$ b

但是这提出了...
1)我将如何确定IProperty是一个工作(或任何儿童财产是一个链接回到父母)



2)它提到了这一点页面使用这将覆盖我的手动覆盖,例如。如果一个非常具体的属性链接需要为NULL。这将是一个问题(如果它仍然是一个问题,但不能没有考虑#1首先考虑)



任何想法呢?我只是错过了什么?





更新1



还是不行。即使以下内容仍然不会在数据库模式中执行Not-Nullable ...

  public class FluentConvention:IPropertyConvention 
{
public void Apply(IPropertyInstance instance)
{
instance.Not.Nullable();


$ / code $ / pre
$ b $ p

它可以用于所有其他字段。 。


/ shrug


有什么想法?





更新2

虽然这不是我正在寻找的答案,但我找到了一个解决方法...
我使用的是NHibernate Validator程序集,在程序集中有一个[NotNull]属性。如果我使用Validator属性来修饰我的类,并在架构创建之前将ValidationEngine关联到NHibernate,那么它会将FK数据库列标记为Not-Nullable(不可空)。

  public class Job //有很多作业
{
public virtual Guid Id {get;组; }
public virtual string Name {get;组; }

[NHibernate.Validator.Constraints.NotNull]
public virtual Group Group {get;组;如果有人需要完整的代码来进行NHibernate + ValidationEngine初始化,那么让我们来让我们来看看如何使用NHibernate + ValidationEngine我知道。
仍然在寻找一种方法来使用纯映射约定路由,尽管如果任何人有任何信息...

谢谢!

解决方案

您可以在Fluenttly.Configure()中覆盖自动映射属性作为AutoMap的一部分。



所以你可以这样做:

  .Override< Job> (map => map.References(x => x.Group).Not.Nullable())



如果你有很多需要这个的类,这不是很方便。

编辑:
您也可以在实现IAutoMappingOverride的类中指定重写,如下所示:

  public class JobMappingOverride:IAutoMappingOverride< Job> 
{
public void Override(AutoMapping< Job> mapping)
{
mapping.References(x => x.Group).Not.Nullable();


$ / code $ / pre

并包含它:



pre $ .UseOverridesFromAssemblyOf< JobMappingOverride>()

这样可以让你的流畅配置更清洁一些。


Just getting my feet wet with some Fluent NHibernate AutoMap conventions, and ran into something I couldn't figure out. I assume I'm just not looking in the right place... Basically trying to enforce NOT-NULL on the "many" side of the one to many relationship. It seems, using the automapping, it always makes the parent property Id nullable in the database.

I did some searching on StackOverFlow and found similar questions, but nothing relating to AutoMapping and Conventions though (unless I missed it).

Quick example...

public class Group    // One Group
{
    public Group() { this.Jobs = new List<Job>(); }
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Job> Jobs { get; protected set; }
}

public class Job    // Has many Jobs
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }

    // Trying to make this field not-nullable in the database.
    public virtual Group Group { get; set; }
}

I thought I'd be able to just create a convention like...

public class OneToManyConvention : IHasOneConvention
{
    public void Apply(IOneToOneInstance instance)
    {
        // Nullable() isn't a valid method...
        instance.Not.Nullable();   
    }
}

But it seems IOneToOnInstance doesn't have a Nullable() method. I can do this if I create a Map file for Job, but trying to avoid any Map files and stick with auto-mapping.

I came across this link on the Fluent group list describing something similar.

Which describes something like this...

public class NotNullPropertyConvention : IPropertyConvention
{
    public bool Accept(IProperty target)
    {
            return true;
    }
    public void Apply(IProperty target)
    {
            target.Not.Nullable();
    }
}

But that raises the questions of... 1) How would I determine IProperty to be a Job (or any child property that is a link back to the parent)

2) It made a mention on that page that using this would override my manual overrides, eg. if a very specific property link needed to be NULL. Which would be an issue (if it's still an issue, but can't test without figuring out #1 first)

Any ideas on this? Am I just missing something?



Update 1

Still no go. Even the following still doesn't enforce Not-Nullable in the database schema...

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

It does for all of the other fields though...
/shrug

Any ideas?



Update 2

While this isn't the answer I was looking for, I did find a work around... I was using NHibernate Validator assembly, and within that assembly there is a [NotNull] attribute. If I decorated my class with the Validator attribute, and associated the ValidationEngine to NHibernate before the schema creation, it would tag the FK database column as Not-Nullable.

public class Job    // Has many Jobs
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }

    [NHibernate.Validator.Constraints.NotNull]
    public virtual Group Group { get; set; }
}

If anyone needs the full code for the NHibernate + ValidationEngine initialization, just let me know. Still looking for a way to do it using the pure mapping convention route though if anyone has any info...

Thanks!

解决方案

You can override the auto-mapped properties as part of your AutoMap in Fluenttly.Configure().

So you can do this:

.Override<Job>(map => map.References(x => x.Group).Not.Nullable())

It's not exactly convenient if you have a lot of classes that need this though.

Edit: You can also specify the override in a class that implements IAutoMappingOverride like so:

    public class JobMappingOverride : IAutoMappingOverride<Job>
    {
            public void Override(AutoMapping<Job> mapping)
            {
                    mapping.References(x => x.Group).Not.Nullable();
            }
    }

and include it like so:

    .UseOverridesFromAssemblyOf<JobMappingOverride>()

This would keep your fluent configuration a little cleaner.

这篇关于流利NHibernate强制外键参考不可空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆