0

C# と SQL Server 2005 を使用して ASP .Net MVC 3 アプリケーションを開発しています。ログインするユーザーのタイプに応じてインターフェイスをカスタマイズしたいと考えています。私は マイクロソフトのサイトでこの チュートに従っていましたが、彼がフィルター クラス (ActionLogFilterAttribute) で ActionLog インスタンスを使用すると行き詰まります。実際、このクラスは「StoreDB.designer.cs」クラスで宣言されていますが、Entity Framework の「コード ファースト」メソッドを使用してプロジェクトを作成したため、このクラスはありません。コンテキストにはこのクラスしかありません:

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);
        }
    }
}

それで、解決策はありますか??

4

1 に答える 1

0

彼が使用している ActionLog インスタンスは、彼の ActionLog テーブルに関連するエンティティです。

あなたがする必要があるのは、独自の ActionLog エンティティを追加することです-それだけです:)

于 2013-05-03T09:32:05.897 に答える