Csharp/C#教程:流畅的NHibernate强制在外键引用上不可为空分享


流畅的NHibernate强制在外键引用上不可为空

刚刚尝试了一些流畅的NHibernate AutoMap惯例,并遇到了一些我无法弄清楚的事情。 我假设我只是没有找到正确的位置…基本上尝试在一对多关系的“多”方面强制执行NOT-NULL。 看来,使用自动化,它总是使父属性Id在数据库中可以为空。

我在StackOverFlow上做了一些搜索并发现了类似的问题,但是没有任何与AutoMapping和Conventions相关的内容(除非我错过了)。

快速举例……

public class Group // One Group { public Group() { this.Jobs = new List(); } public virtual Guid Id { get; set; } public virtual string Name { get; set; } public virtual IList 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; } } 

我以为我能够创建像……这样的会议

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

但似乎IOneToOnInstance没有Nullable()方法。 我可以这样做,如果我为Job创建一个Map文件,但试图避免任何Map文件并坚持使用自动映射。

我在Fluent组列表中看到了这个链接,描述了类似的内容。

其中描述的是这样的……

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

但这提出了以下问题…… 1)我如何确定IProperty是一个Job(或任何一个返回父级的子属性)

2)它在该页面上提到使用它会覆盖我的手动覆盖,例如。 如果一个非常具体的属性链接需要为NULL。 这将是一个问题(如果它仍然是一个问题,但不能先测试#1)

有什么想法吗? 我只是错过了一些东西吗?


更新1

仍然没有去。 即使以下仍然没有在数据库模式中强制执行Not-Nullable ……

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

虽然它适用于所有其他领域…
/耸肩

有任何想法吗?


更新2

虽然这不是我想要的答案,但我确实找到了解决方法……我正在使用NHibernate Validator程序集,并且在该程序集中有一个[NotNull]属性。 如果我使用Validator属性修饰了我的类,并在创建模式之前将ValidationEngine与NHibernate相关联,那么它会将FK数据库列标记为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; } } 

如果有人需要NHibernate + ValidationEngine初始化的完整代码,请告诉我。 如果有人有任何信息,仍在寻找使用纯映射约定路线的方法…

谢谢!

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

所以你可以这样做:

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

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

编辑:您还可以在实现IAutoMappingOverride的类中指定覆盖,如下所示:

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

并包括它:

  .UseOverridesFromAssemblyOf() 

这将使您的流畅配置更清洁。

似乎只对类的简单属性调用IPropertyConvention 。 如果您的属性引用了另一个类,则还需要使用IReferenceConvention

试试这个:

上述就是C#学习教程:流畅的NHibernate强制在外键引用上不可为空分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1001603.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐