我在数据库方法中使用EF Code First,并在asp.net Web应用程序中创建了CRUD操作。现在我想在整个应用程序中过滤一些记录。
我有客户端和产品类
客户端:
public partial class Client
{
public Client()
{
Product = new HashSet<Product>();
}
[Key]
public int ClientID { get; set; }
[Required]
[StringLength(5000)]
public string ClientName { get; set; }
public string Status { get; set; }
public virtual ICollection<Product> Product{ get; set; }
产品:
public partial class Product
{
public Product()
{
}
public int Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public virtual Client Client { get; set; }
我想要的是让产品和客户拥有Status =“Active”
我不想在每个页面上手动执行,而是在类级别定义,以便可以在使用Formview的Insert和Edit.aspx自动生成的动态页面上自动完成。请建议可能的方式。
答案 0 :(得分:0)
我在我的项目中使用了这个:这允许您添加自己的过滤器方法或只添加FilterByAttribute,指定要过滤的列和要过滤的值。这是通过检查getMethod的存在来实现的,其名称类似于GetTableName,并在每个表上进行检查。您需要在Global.asax文件中使用AdvancedMetaModel替换MetaModel。
希望这会有所帮助。
public class AdvancedMetaModel : MetaModel
{
/// <summary>
/// Creates the table.
/// </summary>
/// <param name="provider">The provider.</param>
/// <returns></returns>
protected override MetaTable CreateTable(TableProvider provider)
{
return new AdvancedMetaTable(this, provider);
}
}
public class AdvancedMetaTable : MetaTable
{
private SecureMetaModel SecureModel { get { return (SecureMetaModel)this.Model; } }
public String GetMethodName { get; set; }
private MethodInfo CreateQueryMethod { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="AdvancedMetaTable"/> class.
/// </summary>
/// <param name="metaModel">The meta model.</param>
/// <param name="tableProvider">The table provider.</param>
public AdvancedMetaTable(MetaModel metaModel, TableProvider tableProvider) :
base(metaModel, tableProvider)
{
// set the Get Method Name
var context = base.CreateContext();
var getMethodName = DefaultGetMethodName;
if (context != null && context.HasMethod(DefaultGetMethodName))
{
GetMethodName = getMethodName;
this.CreateQueryMethod = this.DataContextType.GetMethod(GetMethodName);
}
else
GetMethodName = String.Empty;
}
internal String DefaultGetMethodName
{
get { return String.Format("Get{0}", DataContextPropertyName); }
}
public override IQueryable GetQuery(object context)
{
var filterByAttribute = this.GetAttribute<FilterByAttribute>();
if (this.CreateQueryMethod != null)
{
if (context == null)
context = this.CreateContext();
//this.Provider.GetQuery(context);
var query = (IQueryable)this.CreateQueryMethod.Invoke(context, null);
if (this.SortColumn != null)
return query.GetQueryOrdered((MetaTable)this);
else
return query;
}
else if (filterByAttribute != null)
{
MetaColumn filterByColumn = this.GetColumn(filterByAttribute.ColumnName);
var query = base.GetQuery(context).GetQueryFilteredByColumn(filterByColumn, filterByAttribute.ValueWhenTrue);
return query;
}
else
{
var query = base.GetQuery(context);
return query;
}
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class FilterByAttribute:Attribute { //此属性需要使用&#34; AllowMultiple = true&#34;参考David Ebbo //实现时,此标识符仅是属性的类型。然而, //旨在使用唯一标识符来标识两个标识符 //相同类型的属性。 公共覆盖对象TypeId {get {return this; }}
public String ColumnName { get; set; }
public Object ValueWhenTrue { get; set; }
public FilterByAttribute(String columnName, Object valueWhenTrue)
{
ColumnName = columnName;
ValueWhenTrue = valueWhenTrue;
}
答案 1 :(得分:0)
她是自定义get方法的示例
public IQueryable GetStations() { var context = System.Web.HttpContext.Current; var stations = this.Stations.Where(s =&gt; s.Active);
if (!context.User.IsInRole("Acctg") && !context.User.IsInRole("Execs"))
{
var user = MembershipHelpers.GetUser();
var stationIds = user.Stations.Select(s => s.Id).Distinct().ToList();
stations = stations.Where(s => stationIds.Contains(s.Id));
}
return stations;
}