Entity Framework - 检查属性是否已映射

3
我正在使用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);
}

如何找到传递给此事件的属性是否在实体框架中映射的最佳方法是什么?

编辑:我看到了这个问题。但是,它似乎有点过度,不太符合我的需求。

1个回答

9

我的问题出现在Foo类中。我发现有一些[NotMapped]属性浮动到了我需要检查的属性上方。最终我使用了以下代码:

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);
    }
}

谢谢,这非常有用。 - hossein andarkhora

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接