2010-12-13 93 views
1

Fluent NHibernate有一个简单的自动映射实体的方法吗?C#流利nhibernate

比方说,我有一些类,如下面的一个和相应的classmaps:

public sealed class Hello 
{ 
    public String Name { get; set; } 
    public DateTime Timestamp { get; set; } 
} 

public class HelloMapping : ClassMap<Hello> 
{ 
    public HelloMapping() 
    { 
     Not.LazyLoad(); 
     // Some Id here 
     Map(x => x.Name).Not.Nullable().Length(64); 
     Map(x => x.Timestamp).Not.Nullable(); 
    } 
} 

那么,这是否Fluent NHibernate已经像“添加像Hello每个映射实体”

如果没有,让NHibernate使用我提供的映射的最简单方法是什么?

回答

3

这取决于“like”是什么意思?

你是指同一个命名空间中的所有实体吗?那么你可以做

public class MyConfiguration : DefaultAutomappingConfiguration { 
    public override bool ShouldMap(Type type) { 
     return type.Namespace == typeof(Hello).Namespace; 
    } 
} 

无论你是什么意思,你可以设置一个约定来做你正在努力实现的。见auto mapping in Fluent NHibernate

+0

是真的,如果我禁用延迟加载属性不必是虚拟的?因为我猜在这种情况下不会生成延迟加载代理,并且只需访问属性。还是我误会了? (只知道:) – 2010-12-13 22:35:16

+0

@ Yippie-Kai-Yay:我没有注意到你禁用了懒惰加载;你是100%正确的。 – jason 2010-12-13 22:43:03

0

简答题:http://wiki.fluentnhibernate.org/Auto_mapping。您可以使用FluentNH中内置的对象和基本约定来映射不需要太多自定义行为的对象。

您也可以使用继承来定义在大多数或所有类中都具有通用元素的映射。说Hello是一个定义Id,Name和Timestamp的基类。您可以为此基类定义映射,然后直接从其派生出来以产生其他对象的映射,或者可以为应存储在公共表结构中的对象定义JoinedSubclass映射(通常是因为它们是基类的各种风格类,像CheckingAccount,SavingsAccount和MoneyMarketAccount都是具有基本类似数据结构的不同类型的BankAccounts)。