Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 363
Charles W Avatar asked Dec 20 '25 01:12

Charles W


1 Answers

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);
    }
}
like image 63
Charles W Avatar answered Dec 23 '25 01:12

Charles W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!