本文介绍了如何微调 FluentNHibernate 的自动映射器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以昨天我设法获得了 NHibernate 和 FluentNHibernate 的最新主干版本,以用于我最新的小项目.(我正在开发一个错误跟踪应用程序.)我使用存储库模式创建了一个很好的数据访问层.

Okay, so yesterday I managed to get the latest trunk builds of NHibernate and FluentNHibernate to work with my latest little project. (I'm working on a bug tracking application.) I created a nice data access layer using the Repository pattern.

我决定我的实体没什么特别的,而且随着 ORM 当前的成熟,我不想手工制作数据库.因此,我选择使用 FluentNHibernate 的自动映射功能,并将 NHibernate 的hbm2ddl.auto"属性设置为create".

I decided that my entities are nothing special, and also that with the current maturity of ORMs, I don't want to hand-craft the database.So, I chose to use FluentNHibernate's auto mapping feature with NHibernate's "hbm2ddl.auto" property set to "create".

它真的很有魅力.我将 NHibernate 配置放在我的应用程序域的配置文件中,进行设置,然后开始使用它.(目前,我只创建了一些单元测试.)它创建了数据库中的所有表,以及我需要的一切.它甚至正确地映射了我的多对多关系.

It really works like a charm. I put the NHibernate configuration in my app domain's config file, set it up, and started playing with it. (For the time being, I created some unit tests only.) It created all tables in the database, and everything I need for it. It even mapped my many-to-many relationships correctly.

但是,有一些小故障:

  • 在数据库中创建的所有列都允许为空.我知道它无法预测哪些属性应该允许 null 哪些不应该,但至少我想告诉它它应该只允许 null 对于那些在 .NET 中有意义的类型(例如非-nullable 值类型不应允许为 null).
  • 它创建的所有 nvarchar 和 varbinary 列的默认长度为 255.我更愿意将它们设为最大值而不是最大值.

有没有办法告诉自动映射器上面两个简单的规则?

Is there a way to tell the auto mapper about the two simple rules above?

如果答案是否定的,如果我修改它创建的表,它会正常工作吗?(因此,如果我将某些列设置为不允许为空,并更改其他一些列的允许长度,它会正确地与它们一起使用吗?)

If the answer is no, will it work correctly if I modify the tables it created?(So, if I set some columns not to allow null, and change the allowed length for some other, will it correctly work with them?)

最终非常感谢所有路过并提供帮助的人.我所有的 Fluent 问题现在都解决了.

FINALBig Thanks to everyone who dropped by and helped out.All of my issues with Fluent are solved now.

推荐答案

您可以使用 Auto Mapping Overrides 来更改 Auto Mapper 的工作方式,并且您还可以定义将被自动映射器使用的约定.

You can use Auto Mapping Overrides to change how the Auto Mapper work, and you can also define Conventions, that will be used instead by the auto mapper.

这是一个关于如何使用约定和覆盖的示例:

Here is an example on how to use both the conventions and the overrides:

var mappings = new AutoPersistenceModel();
mappings.Conventions.Setup(s => s.Add<ColumnNullabilityConvention>());
mappings.UseOverridesFromAssemblyOf<AssemblyName>();

// This convention will set all properties to be not nullable

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

// This override will change "string" to use "text" instead of "varchar(255)".
// Also set the property to be not nullable

public class SomeOverrideInTheSameAssembly : IAutoMappingOverride<TypeName>
{
   public void Override(AutoMapping<TypeName> mapping)
   {
       mapping.Map(x => x.Property).CustomType("StringClob").CustomSqlType("text");
       mapping.Map(x => x.Property).Not.Nullable();
   }
}

查看这些链接以获取更多示例:

Check these links for more examples:

这篇关于如何微调 FluentNHibernate 的自动映射器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:52