我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。我想根据登录的用户类型自定义我的界面。我在微软网站上关注 这个tuto ,当他在过滤器类(ActionLogFilterAttribute)中使用ActionLog实例时我被卡住了。事实上,这个类是在我没有的“StoreDB.designer.cs”类中声明的,因为我使用实体框架的“代码优先”方法创建了我的项目。我只有这个类的上下文:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace MvcApplication2.Models
{
public class GammeContext : DbContext
{
public GammeContext()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<GammeContext>());
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Ns_AFaire> Ns_AFaires { get; set; }
public DbSet<Famille> Familles { get; set; }
public DbSet<Fonction> Fonctions { get; set; }
public DbSet<Fonction_Poste> Fonction_Postes { get; set; }
public DbSet<Gamme> Gammes { get; set; }
public DbSet<Historique> Historiques { get; set; }
public DbSet<Ligne> Lignes { get; set; }
public DbSet<Phase> Phases { get; set; }
public DbSet<Poste> Postes { get; set; }
public DbSet<Produit> Produits { get; set; }
public DbSet<Profile_Ga> Profil_Gas { get; set; }
public DbSet<Sous_Famille> Sous_Familles { get; set; }
public DbSet<UF> UFs { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Num_Serie> Num_Series { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
}
}
}
这是我创建的过滤器类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Filters
{
public class ActionLogFilterAttribute : ActionFilterAttribute, IActionFilter
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
GammeContext db = new GammeContext();
ActionLog log = new ActionLog()
{
Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
Action = filterContext.ActionDescriptor.ActionName,
IP = filterContext.HttpContext.Request.UserHostAddress,
DateTime = filterContext.HttpContext.Timestamp
};
db.AddToActionLogs(log);
db.SaveChanges();
base.OnActionExecuting(filterContext);
}
}
}
那么,有什么解决办法吗??