(流利)NHibernate:将完整的表映射到一个对象 [英] (Fluent) NHibernate: Map complete table to one object
本文介绍了(流利)NHibernate:将完整的表映射到一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
设置
关键字|值
--------------- | -------------------
SomeSetting | Foo
AnotherSetting | Bar
... | ...
并且希望将其映射到以下对象:
public class Settings
{
public virtual string SomeSetting {get;组; }
公共虚拟字符串AnotherSetting {get;组;如何使用(Fluent)NHibernate来实现这个功能?解决方案我会将键/值对映射到私有IDictionary,并通过访问字典来显示属性。请参阅 Ayende在地图上的博客条目以创建字典。例如:
public class Settings
{
private IDictionary< string,string> _dict;
//在构造函数中初始化字典
public Settings()
{
_dict = new Dictionary< string,string> {{SomeSetting,string.Empty}};
public virtual string SomeSetting
{
get {return _dict [SomeSetting]; }
set {_dict [SomeSetting] = value; }
}
//等等
}
I have the following database table:
Settings
Key | Value
---------------|-------------------
SomeSetting | Foo
AnotherSetting | Bar
... | ...
And want to map this to the following object:
public class Settings
{
public virtual string SomeSetting { get; set; }
public virtual string AnotherSetting { get; set; }
}
How could I accomplish this using (Fluent) NHibernate?
解决方案 I would map the key/value pairs to a private IDictionary and expose the properties by accessing the dictionary. See Ayende's blog entry on map for creating the dictionary. Something like:
public class Settings
{
private IDictionary<string, string> _dict;
//initialize dictionary in constructor
public Settings()
{
_dict = new Dictionary<string, string> { {"SomeSetting", string.Empty} };
}
public virtual string SomeSetting
{
get { return _dict["SomeSetting"]; }
set { _dict["SomeSetting"] = value; }
}
// etc.
}
这篇关于(流利)NHibernate:将完整的表映射到一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文