Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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# LinqtoSql中的链接方法:如何获取实体上下文_C#_Linq_Generics_Audit - Fatal编程技术网

C# LinqtoSql中的链接方法:如何获取实体上下文

C# LinqtoSql中的链接方法:如何获取实体上下文,c#,linq,generics,audit,C#,Linq,Generics,Audit,我有一个与我合作的审计库。我相信这个问题更多地与泛型有关,因为我不确定如何编写类似于所讨论方法的方法 我有以下“链接”方法,似乎无法获取查找表的上下文: AuditProperty(m => m.StationTypeId) .GetValueFrom(stationtypeId => GetStationType(stationtypeId)) .WithPropertyName("StationType"); 其思想是将一个ID传递到GetValueFrom()linq方法中,

我有一个与我合作的审计库。我相信这个问题更多地与泛型有关,因为我不确定如何编写类似于所讨论方法的方法

我有以下“链接”方法,似乎无法获取查找表的上下文:

AuditProperty(m => m.StationTypeId)
.GetValueFrom(stationtypeId => GetStationType(stationtypeId))
.WithPropertyName("StationType");
其思想是将一个ID传递到GetValueFrom()linq方法中,并从(在本例中)stationtype表返回一个字符串。通过在运行时将静态表分配给实际的Stationtype表(下面是stationTypeTable),我可以执行如下查找:

public string GetStationType(int? stationTypeID)
        {

            var stationtype = stationTypeTable.FirstOrDefault(st => object.Equals(st.Id, stationTypeID));
            return stationtype != null ? stationtype.Value : String.Empty;
        }
我知道这是个坏习惯。当某些主键ID不存在时,我会遇到异常。但是,当我调用linq方法时,我实际上似乎无法获得任何表的上下文。你知道我该怎么做吗?以下是linq方法,供您参考:

public class EntityAuditConfiguration<TEntity> : EntityAuditConfiguration
{
    /// <summary>
    /// Customize the default behavior when auditing a specific property
    /// </summary>
    public CustomPropertyAuditor<TEntity, T> AuditProperty<T>(Expression<Func<TEntity, T>> propertySelector)
    {
        var config = new CustomPropertyAuditor<TEntity, T>();
        CustomizedProperties.Add(propertySelector.ToPropertyInfo(), config);
        return config;
    }

    /// <summary>
    /// Include an association (relationship) table to audit along with the parent table
    /// </summary>
    public RelatationshipConfiguration<TChildEntity, TEntity> AuditMany<TChildEntity>(Expression<Func<TEntity, IEnumerable<TChildEntity>>> selector)
        where TChildEntity : class
    {
        var relationship = new RelatationshipConfiguration<TChildEntity, TEntity>();
        ((IEntityAuditConfiguration) this).Relationships.Add(relationship);
        return relationship;
    }
}



public class CustomPropertyAuditor<TEntity, TProp> : IPropertyAuditor
{
    private Expression<Func<TProp, string>> _propertySelector;
    private string _propertyName;

    public CustomPropertyAuditor<TEntity, TProp> WithPropertyName(string propertyName)
    {
        if (propertyName == null) throw new ArgumentNullException("propertyName");
        _propertyName = propertyName;
        return this;
    }


    public CustomPropertyAuditor<TEntity, TProp> GetValueFrom(Expression<Func<TProp, string>> valueSelector)
    {
        if (valueSelector == null) throw new ArgumentNullException("valueSelector");
        _propertySelector = valueSelector;
        return this;
    }

    AuditedProperty IPropertyAuditor.AuditProperty(PropertyInfo property, object oldValue, object newValue)
    {
        var auditedProperty = new AuditedProperty(_propertyName ?? property.Name);
        var func = _propertySelector.Compile();

        if (oldValue != null)
            auditedProperty.OldValue = func.DynamicInvoke(oldValue).ToString();

        if (newValue != null)
            auditedProperty.NewValue = func.DynamicInvoke(newValue).ToString();

        return auditedProperty;
    }
}
公共类EntityAuditConfiguration:EntityAuditConfiguration { /// ///自定义审核特定属性时的默认行为 /// 公共CustomPropertyAuditor AuditProperty(表达式属性选择器) { var config=new CustomPropertyAuditor(); 添加(propertySelector.ToPropertyInfo(),config); 返回配置; } /// ///包括要与父表一起审核的关联(关系)表 /// public RelationshipConfiguration AuditMany(表达式选择器) 学生身份:班级 { var relationship=newrelationshipConfiguration(); ((IEntityAuditConfiguration)this).Relationships.Add(relationship); 回报关系; } } 公共类CustomPropertyAuditor:IPropertyAuditor { 私有表达式属性选择器; 私有字符串_propertyName; 公共CustomPropertyAuditor with propertyName(字符串propertyName) { 如果(propertyName==null)抛出新的ArgumentNullException(“propertyName”); _propertyName=propertyName; 归还这个; } 公共CustomPropertyAuditor GetValueFrom(表达式值选择器) { 如果(valueSelector==null)抛出新的ArgumentNullException(“valueSelector”); _propertySelector=值选择器; 归还这个; } AuditedProperty IPropertyAuditor.AuditProperty(PropertyInfo属性、对象oldValue、对象newValue) { var auditedProperty=new auditedProperty(_propertyName??property.Name); var func=_propertySelector.Compile(); if(oldValue!=null) auditedProperty.OldValue=func.DynamicInvoke(OldValue.ToString(); if(newValue!=null) auditedProperty.NewValue=func.DynamicInvoke(NewValue).ToString(); 归还被审计财产; } }
谢谢

我只是创建了一个列表,并在类的oncreate()方法中将每一行添加到列表中。这些查找表应该在上下文中可用,我不必创建单独的查找列表(但现在它可以100%工作)。有谁有其他更好的想法吗?

至少给我一个参考资料,让我可以确切地了解如何创建这些类型的方法!我通过简单地创建一个列表并在类的oncreate()方法中将每一行添加到列表中来修复它。这些查找表应该在上下文中可用,我不必创建单独的查找列表。有人有其他想法吗?我想不是吧?