如何在NHibernate映射类中找到未映射的属性? [英] How to find unmapped properties in a NHibernate mapped class?

查看:113
本文介绍了如何在NHibernate映射类中找到未映射的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚遇到一个与NHibernate相关的问题,我忘了映射一个类的一个属性.

I just had a NHibernate related problem where I forgot to map one property of a class.

一个非常简单的示例:

public class MyClass
{
    public virtual int ID { get; set; }
    public virtual string SomeText { get; set; }
    public virtual int SomeNumber { get; set; }
}

...以及映射文件:

...and the mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyAssembly"
                   namespace="MyAssembly.MyNamespace">

    <class name="MyClass" table="SomeTable">
        <property name="ID" />
        <property name="SomeText" />      
    </class>

</hibernate-mapping>

在这个简单的示例中,您可以立即看到问题:
在类中有一个名为"SomeNumber"的属性,但在映射文件中没有.
因此NHibernate不会将其映射,并且始终为零.

In this simple example, you can see the problem at once:
there is a property named "SomeNumber" in the class, but not in the mapping file.
So NHibernate will not map it and it will always be zero.

真正的类具有更多的属性,因此问题不那么容易发现,即使我100%确信数据库中的值是!=零.

The real class had a lot more properties, so the problem was not as easy to see and it took me quite some time to figure out why SomeNumber always returned zero even though I was 100% sure that the value in the database was != zero.

所以,这是我的问题:

是否有一些简单的方法可以通过NHibernate找出答案?
就像在映射类时的编译器警告一样,但某些属性不是.
或一些我可以运行的查询,它向我显示了映射类中的未映射属性...您明白了.

Is there some simple way to find this out via NHibernate?
Like a compiler warning when a class is mapped, but some of its properties are not.
Or some query that I can run that shows me unmapped properties in mapped classes...you get the idea.

(此外,如果我可以排除一些我确实不想映射的遗留列,那就太好了.)

(Plus, it would be nice if I could exclude some legacy columns that I really don't want mapped.)


好的,我查看了您提出的所有内容,并决定使用元数据API……这对我来说似乎最容易理解.
现在,我知道要搜索的内容了,我找到了一些帮助我入门的示例.
到目前为止,我有这个:


Okay, I looked at everything you proposed and decided to go with the meta-data API...that looks the easiest to understand for me.
Now that I know what to search for, I found some examples which helped me to get started.
So far, I have this:

Type type = typeof(MyClass);

IClassMetadata meta = MySessionFactory.GetClassMetadata(type);

PropertyInfo[] infos = type.GetProperties();

foreach (PropertyInfo info in infos)
{
    if (meta.PropertyNames.Contains(info.Name))
    {
        Console.WriteLine("{0} is mapped!", info.Name);
    }
    else
    {
        Console.WriteLine("{0} is not mapped!", info.Name);
    }
}

它几乎可以工作,除了以下几点: IClassMetadata.PropertyNames返回 ID以外的所有属性的名称.
要获取ID,我必须使用IClassMetadata.IdentifierPropertyName.

It nearly works, except one thing: IClassMetadata.PropertyNames returns the names of all the properties except the ID.
To get the ID, I have to use IClassMetadata.IdentifierPropertyName.

是的,我可以将.PropertyNames保存在一个新数组中,向其中添加.IdentifierPropertyName并搜索那个数组.
但这对我来说很奇怪.
有没有更好的方法来获取所有映射的属性(包括ID)?

Yes, I could save .PropertyNames in a new array, add .IdentifierPropertyName to it and search that array.
But this looks strange to me.
Is there no better way to get all mapped properties including the ID?

推荐答案

您可以使用NHibernate元数据API查找映射的属性,并使用反射查找所有属性.

You could use the NHibernate meta-data API to find the mapped properties, and reflection to find all the properties.

修改,不,没有其他方法可以列出包括ID在内的所有属性.使用起来并不难:

Edit No, there isn't any other way list all the properties including the id. It isn't that hard to use:

foreach (PropertyInfo info in infos)
{
    if (meta.PropertyNames.Contains(info.Name) || info.Name = meta.IdentifierPropertyName)
    {
        Console.WriteLine("{0} is mapped!", info.Name);
    }
    else
    {
        Console.WriteLine("{0} is not mapped!", info.Name);
    }
}

这篇关于如何在NHibernate映射类中找到未映射的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆