Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 5获取DbEntry的DisplayFormatAttribute_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# MVC 5获取DbEntry的DisplayFormatAttribute

C# MVC 5获取DbEntry的DisplayFormatAttribute,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个审计方法记录对datatabase的更改 该方法看起来(有点)简化如下 private List<Case_History> GetRecords(DbEntityEntry dbEntry, ApplicationUser user, int actionID) { List<Case_History> result = new List<Case_History>(); DateTime changeTime = DateTime

我有一个审计方法记录对datatabase的更改

该方法看起来(有点)简化如下

private List<Case_History> GetRecords(DbEntityEntry dbEntry, ApplicationUser user, int actionID)
{
    List<Case_History> result = new List<Case_History>();

    DateTime changeTime = DateTime.Now;

    // Finds the table
    TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;

    // Finds the table name
    string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().BaseType.Name;

    // Finds primary key
    string keyName = dbEntry.Entity.GetType().GetProperties().Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Count() > 0).Name;

else if (dbEntry.State == EntityState.Modified)
{
    List<string> values = new List<string>();

    foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
    {
        if (!object.Equals(dbEntry.OriginalValues.GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName)) && propertyName != "Modified_date")
        {
            //DEBUGGING VARIABLE
            var originalValue = dbEntry.OriginalValues.GetValue<object>(propertyName);
            //DEBUGGING VARIABLE
            var newValue = dbEntry.CurrentValues.GetValue<object>(propertyName);

            //Here is the part where i want to get the column display name
            // This code is not working
            PropertyInfo prop = typeof(this).GetProperty(propertyName);
            var att = (DisplayAttribute)prop.GetCustomAttributes(typeof(DisplayAttribute);
            if (att != null)
            {
                //found Display name
            }

            //If the record is different, record the change
            if (dbEntry.OriginalValues.GetValue<object>(propertyName) != null && dbEntry.CurrentValues.GetValue<object>(propertyName) != null)
            {
                    values.Add(propertyName + ": " + dbEntry.OriginalValues.GetValue<object>(propertyName).ToString() + " -> " + dbEntry.CurrentValues.GetValue<object>(propertyName).ToString());

            }
        }
    }
}
private List GetRecords(DbEntityEntry dbEntry,ApplicationUser用户,int actionID)
{
列表结果=新列表();
DateTime changeTime=DateTime.Now;
//找到表
TableAttribute tableAttr=dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute),false)。SingleOrDefault()作为TableAttribute;
//查找表名
字符串tableName=tablettr!=null?tablettr.Name:dbEntry.Entity.GetType().BaseType.Name;
//查找主键
字符串keyName=dbEntry.Entity.GetType().GetProperties().Single(p=>p.GetCustomAttributes(typeof(KeyAttribute),false).Count()>0.Name;
else if(dbEntry.State==EntityState.Modified)
{
列表值=新列表();
foreach(dbEntry.OriginalValues.PropertyNames中的字符串propertyName)
{
如果(!object.Equals(dbEntry.OriginalValues.GetValue(propertyName),dbEntry.CurrentValues.GetValue(propertyName))&&propertyName!=“修改日期”)
{
//调试变量
var originalValue=dbEntry.OriginalValues.GetValue(propertyName);
//调试变量
var newValue=dbEntry.CurrentValues.GetValue(propertyName);
//下面是我想要获取列显示名称的部分
//此代码不起作用
PropertyInfo prop=typeof(this).GetProperty(propertyName);
var att=(DisplayAttribute)prop.GetCustomAttributes(typeof(DisplayAttribute);
如果(att!=null)
{
//找到显示名称
}
//如果记录不同,则记录更改
if(dbEntry.OriginalValues.GetValue(propertyName)!=null&&dbEntry.CurrentValues.GetValue(propertyName)!=null)
{
values.Add(propertyName+“:”+dbEntry.OriginalValues.GetValue(propertyName.ToString()+“->”+dbEntry.CurrentValues.GetValue(propertyName.ToString());
}
}
}
}
我在调试会话中的本地变量字段的元数据属性中找到了该变量。但仅在“this”变量中找到。对于每个不同的DBentries,该变量必须是动态的。

只需替换即可

PropertyInfo prop = typeof(this).GetProperty(propertyName);


实际上,“this”是当前的类,它不包含要记录的实体的属性

为什么不将typeof(this)替换为dbEntry.entity.GetType()?啊,哇,是的,很简单:)非常感谢。
PropertyInfo prop = dbEntry.Entity.GetType().GetProperty(propertyName);