如何微调FluentNHibernate的自动映射器?映射器、FluentNHibernate

2023-09-02 20:49:27 作者:梨窝浅笑

好了,昨天我设法获得最新的主干建立NHibernate和FluentNHibernate与我的最新的小项目工作。 (我工作的一个缺陷跟踪应用程序)。我创建了一个不错的数据访问层使用Repository模式。

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.

我决定,我的实体都没有什么特别的,也与奥姆斯目前的成熟度,我不想手工工艺数据库。 所以,我选择使用FluentNHibernate的自动映射功能与NHibernate的hbm2ddl.auto属性设置为创造。

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.

然而,也有一些小故障:

However, there are a few small glitches:

所有的数据库创建的列允许空。我明白,它不能predict其性能应允许空,哪些不应该,但至少我想告诉它,它应该允许空只对那些类型而空是有道理的。NET(例如,非空值类型不应该允许为空)。 在它所创建的数据类型为nvarchar和varbinary列,有255默认长度我想preFER有他们的最大替代那种。

有没有办法告诉自动映射关于两个简单的规则之上?

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?)

最后编辑: 非常感谢大家谁下降和帮助了。 我所有的问题用流利现在都解决了。

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

推荐答案

您可以使用自动映射覆盖来更改自动映射工作,你也可以定义约定,将用于代替由自动映射器。

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:

How设置惯例,并覆盖 一些约定将覆盖 功能NHibernate约定常见问题解答 Another SO链接关于这个话题 How to setup conventions and overrides Some convention overrides Fluent NHibernate Conventions FAQ Another SO link about this topic