ActionFilterAttributeを継承するフィルタが好きなように聞こえます。私の場合、これは私が持っている最も単純な例です。これは私のモデルです。属性がデータベースへのマッピングを指示することに注意してください。私の場合は
[Table("UserProfile")]
public class UserProfile
{
[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
それは歴史的ではなかったが、それは、次のように簡単だった:
public sealed class UsersContext : DbContext
{
public UsersContext() : base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
LazyInitializer.EnsureInitialized(ref _initializer, ref isInitialized, ref initializerLock);
}
public void CheckDatabase()
{
Database.SetInitializer<YourDBContextType>(null);
using (var context = new YourDBContextType())
{
if (!context.Database.Exists())
{
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
// Uses your connection string to build the following table.
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
最終的な結果は、最初だけEFされているコードではないですが、またのために、あなたのモデルが可能になります複雑なエンティティから派生したプリミティブを使用するビューさて、あなたが別のものを持っている場合、歴史的なDBContextを言えば、テキスト変換ファイルを変更するか、エンティティの基本クラスを作成することをお勧めします。どちらの方法でも、テーブルに挿入してそのエンティティにフォローアップし、履歴モデルにクローンして保存することができるコードを簡単に生成できます。すべてのことが言われている、私は、フレームワークではなく、制約、トリガーなどに集中してデータベースの最初のアプローチのファンです。
監査に関する興味深い投稿があります[こちら](http://blogs.msdn.com/b/simonince/archive/2009/04/20/auditing-data-changes-in-the-entity-framework- part-2.aspx)、どう思いますか? – ibiza