简体   繁体   English

实体框架-检查属性是否已映射

[英]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. 我正在使用Entity Framework并触发属性更改事件,如果映射了更改的属性,我想在其中更新属性。

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. 我的问题是在Foo课堂上。 I apparently had a floating [NotMapped] attribute far above the property I was checking. 我显然在我要检查的属性上方有一个[NotMapped]浮动属性。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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