简体   繁体   中英

Entity Framework - Check if property is mapped

I'm using Entity Framework and am triggering a property changed event, where I'd like to update a property if the property that changed is mapped.

protected void FooPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    var notMappedArray = typeof(Foo).GetProperty(e.PropertyName).GetCustomAttributes(typeof(NotMappedAttribute), false); // I thought this might return null if the property did not have the attribute. It does not.
    //if (notMappedArray == null)
    UnitOfWork.Value.GetRepository<Foo>().Update(MyFoo);
}

What is the best way to find if a property sent to this event is mapped in entity framework?

Edit: I've seen this question . However, it seems like the answer is going a little overboard, and doesn't quite do what I need.

My problem was in my Foo class. I apparently had a floating [NotMapped] attribute far above the property I was checking. I ended up using something like this:

protected void FooPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    var notMapped = typeof(Foo).GetProperty(e.PropertyName).GetCustomAttributes(typeof(NotMappedAttribute), false);
    if (notMapped.Length == 0)
    {
        UnitOfWork.Value.GetRepository<Foo>().Update(MyFoo);
    }
}

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