我写了一个小控制台应用程序,它应该写出给定类的属性。
它适用于“普通”类,但是当我尝试传入NHibernate classMapping类时,它只显示一个空白列表。
单步执行Visual Studio中的调试器,我只看到空值和一个非显示停止错误:“无法发现有关此对象的更多信息。”
有没有办法获取ClassMapping类中使用的参数/属性?
谢谢!
映射类:
public class CorporateDataMapping : ClassMapping<CorporateData>
{
public CorporateDataMapping()
{
Schema("data");
this.EnhancedSequenceId(x => x.Id);
ManyToOne(x => x.CEO, pm => pm.NotNullable(true));
Property(x => x.Sign, pm => pm.NotNullable(true));
Property(x => x.WeatherType, pm => pm.NotNullable(true));
Property(x => x.EncodingLanguage);
Property(x => x.ZipCode);
Property(x => x.PhysicalLocation);
Property(x => x.Extension, pm => pm.NotNullable(true));
Property(x => x.DisplayAllYear, pm => pm.NotNullable(true));
Property(x => x.Graphics, pm => pm.NotNullable(true));
Property(x => x.InstallDate, pm => pm.NotNullable(true));
}
}
支持显示该类所有属性的程序:
static void Main(string[] args)
{
CorporateDataMapping mapping = new CorporateDataMapping();
printProperties(mapping);
Console.ReadLine();
}
public static void printProperties(Object jsonObject)
{
JObject json = JObject.FromObject(jsonObject);
Console.WriteLine("Classname: {0}\n", jsonObject.ToString());
Console.WriteLine("{0,-20} {1,5}\n", "Name", "Value");
foreach (JProperty property in json.Properties()) {
Console.WriteLine("{0,-20} {1,5:N1}", property.Name, property.Value);
}
}
}
答案 0 :(得分:1)
映射类的属性不会成为映射器的一部分,因此无法通过mapper类上的反射来获取它们。阅读ModelMapper NHibernate类。一些用途显示在以下链接中(您可以通过googling&#34; nhibernate modelmapper beforemapproperty&#34;)找到更多用途:
http://nhibernate.info/blog/2011/09/04/using-nh3-2-mapping-by-code-for-automatic-mapping.html
Nhibernate : Map all decimals with the same precision and scale