我在項目中使用NHibernate 3.1和Fluent NHibernate作爲ORM。我需要一個被Fluent NHibernate忽略的POCO屬性。起初,我的文章可能看起來像this question的確切副本,但事實並非如此。Fluent NHibernate忽略ClassMap內部的屬性,使用FluentMappings
我的複雜問題首先來自POCO是在與映射不同的組件中定義的事實,而我正在使用流利的映射作爲我的POCO。我有額外的要求,不要寫會話工廠配置發生的ingore屬性代碼(這發生在模塊外部的集中位置),而是作爲定義映射的模塊的一部分。理想情況下,我相信正確的地方將是具體的實現,因爲它知道如何向ORM描述POCO。
但是,我堅持這主要是因爲這是我對NHibernate及其流暢的API的第一次影響。到目前爲止,我對它的能力和可擴展性有很好的印象,我希望有一種方法可以通過映射相關代碼封裝在相應的模塊中來實現我的要求。
這裏是我的配置,從一個集中的地方:
List<Assembly> assemblies = GetModules().Select(x => x.GetType().Assembly).ToList();
ISessionFactory nhibernateSessionFactory = Fluently
.Configure()
.Mappings(m => assemblies.ForEach(asm => m.FluentMappings.AddFromAssembly(asm)))
.Database(
MsSqlConfiguration.MsSql2005
.ShowSql()
.ConnectionString(DatabaseConfig.Instance.ConnectionString))
.ExposeConfiguration(c => new SchemaUpdate(c).Execute(true, true))
.BuildSessionFactory();
我使用標準類的映射,從ClassMap
繼承:
public class User
{
public virtual int ID { get; set; }
public virtual String Username { get; set; }
public virtual String Password { get; set; }
public virtual DateTime DateCreated { get; set; }
public virtual DateTime DateModified { get; set; }
// Must ignore
public string ComputedProperty { get { ... } }
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("User");
Id(x => x.ID).GeneratedBy.Identity();
Map(m => m.Username).Not.Nullable().Length(255).UniqueKey("User_Username_Unique_Key");
Map(m => m.Password).Not.Nullable().Length(255);
Map(m => m.DateCreated).Not.Nullable();
Map(m => m.DateModified).Not.Nullable();
}
}
你的映射類是什麼樣的,你的配置是什麼樣的? – shanabus 2012-03-15 20:30:06
我通過添加一些代碼重新訪問了我的問題 – 2012-03-15 20:44:27
另請參閱:http://stackoverflow.com/questions/5830649/how-do-you-make-nhibernate-ignore-a-property-in-a-poco – caspian311 2013-04-19 14:43:20