0
我正在開發一個使用C#和SQL Server 2005的ASP.Net MVC 3應用程序。 我想根據記錄的用戶類型定製我的界面。 我在微軟網站跟隨 this tuto ,當我在過濾器類(ActionLogFilterAttribute)中使用ActionLog實例時,我被卡住了。 Infact,這個類是在'StoreDB.designer.cs'類中聲明的,我沒有,因爲我使用實體框架的'代碼優先'方法創建了我的項目。 我只有這一類上下文:如何篩選ASP MVC中的日誌記錄?
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);
}
}
}
那麼,有沒有什麼辦法解決?
THX你的答案,,,但可我怎麼知道? – anouar 2013-05-03 22:34:25
您可以添加模型和映射器類,並添加一個'public DbSet ActionLogs {get;組; }給你的GammeContext。 –
Dietz
2013-05-06 06:15:08