简体   繁体   中英

Retrieve property name from Fluently mapped column name

Background

I have a class called 'Dog', which references another class called 'Tail', which has a property called 'Size'. So, if i wanted to know the dog's tail size, it would be 'Dog.Tail.Size'. Perfect.

I have this mapped with FluentNHibernate Like this:

 public class DogMap : ClassMap<Dog>
 {
    public DogMap()
    { 
        ... other things here
        Component(x => x.Tail, t => {
            t.Map(x => x.Size, "DG_TL_SIZE").Length(2).Not.Nullable();
        }
    }
 }

The question

Having the column name "DG_TL_SIZE", how can i get "Dog.Tail.Size"? I know i can, after i have Dog.Tail.Size, get the "persistentClass.GetRecursiveProperty("Dog.Tail.Size")" to retrieve the property and work with it.

The thing is that i need to change its value, therefore i need a PropertyInfo from that, but that should not be hard to get when i have "Dog.Tail.Size".

So, how can i get the "Dog.Tail.Size"?

you can iterate through all properties, the code presented needs refactoring though

foreach (var prop in persistentclass.PropertyClosureIterator)
{
    IValue property = prop.Value;
    if (prop.IsComposite)
    {
        var component = (NHibernate.Mapping.Component)prop.Value;

        foreach (var prop2 in component.PropertyIterator)
        {
            foreach (var column in prop2.ColumnIterator)
            {
                if (column.Text == "my Column")
                {
                    // do something with the 'prop2'
                }
            }
        }
    }
    else
    {
        foreach (var column in prop.ColumnIterator)
        {
            if (column.Text == "my Column")
            {
                // do something with the 'prop'
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM