当属性存在时,EntityEntry.Property()会抛出InvalidOperationException

时间:2016-11-11 08:19:44

标签: c# entity-framework asp.net-core entity-framework-core .net-core

大家好,我有以下几个类:

var a1 = new EntityA {Desc = "a1"};
var a2 = new EntityA {Desc = "a2"};
dbx.EntityAs.Add(a1);
dbx.EntityAs.Add(a2);

var b1 = new EntityB { EntityAId = a1.Id };
dbx.EntityBs.Add(b1);
dbx.SaveChanges();
b1.EntityAId = a2.Id;
dbx.SaveChanges();

我有以下运行时代码:

foreach (var entity in changedEntites)
{
var entityType = entity.Entity.GetType();

if (entity.State == EntityState.Modified)
{                  
    var properties = entityType.GetProperties();
    var props = new List<object>();
    foreach (var prop in properties)
    {
        if(entityType.GetProperty(prop.Name) == null)
            continue;
        var pp = entityType.GetProperty(prop.Name);
        if(pp.GetValue(entity.Entity) == null)
            continue;

        var p = entity.Property(prop.Name);
        if (p.IsModified)
            props.Add(new { f = prop.Name, o = p.OriginalValue, c = p.CurrentValue });
    }
}
}

我在DbContext.SaveChanges()方法中修改了代码,如下所示,试图查找实体中哪个属性已更改及其前后值:

var p = entity.Property(prop.Name);

有问题的代码就在这一行:

InvalidOperationException

抛出The property 'EntityA' on entity type 'EntityB' could not be found. Ensure that the property exists and has been included in the model.

entityType.GetProperty(prop.Name)

我的问题是为什么即使entityType.GetProperty(prop.Name).GetValue(entity.Entity)entity.Property()不为空,var p = entity.Property(prop.Name);仍然无法找到该属性?

我可以用try-catch块包围document.getElementById("FirstDiv").remove(); 并忽略异常,但让异常继续投入审计方案并不是一件好事。它也会影响性能。

非常感谢任何解决方法。感谢

2 个答案:

答案 0 :(得分:2)

问题是Property方法在使用navigation属性调用它时只支持原始属性。

您可以使用通过EntityEntry.Metadata属性访问的ER Core元数据服务,该属性返回IEntityType。在您的情况下,使用FindProperty方法,尽管您应该首先使用GetProperties而不是反射:

if (entity.Metadata.FindProperty(prop.Name) == null)
    continue;

var p = entity.Property(prop.Name);
if (p.IsModified)
    props.Add(new { f = prop.Name, o = p.OriginalValue, c = p.CurrentValue });

答案 1 :(得分:0)

这是因为entityEntityEntry。你应该正确命名你的变量,以免混淆:

foreach (var entiry in changedEntries)
{
    var entity = entiry.Entity;
    var entityType = entity.GetType();

    if (entity.State == EntityState.Modified)
    {                  
        var properties = entityType.GetProperties();
        var props = new List<object>();
        foreach (var prop in properties)
        {
            if(entityType.GetProperty(prop.Name) == null)
                continue;
            var pp = entityType.GetProperty(prop.Name);
            if(pp.GetValue(entity) == null)
                continue;

            var p = entity.Property(prop.Name);
            if (p.IsModified)
                props.Add(new { f = prop.Name, o = p.OriginalValue, c = p.CurrentValue });
        }
    }
}