在Create / Update / Delete中插入History表的存储过程?

时间:2015-03-13 19:07:14

标签: c# oracle entity-framework stored-procedures dbcontext

我正在开发MVC5 / EF Code-First应用程序。有没有人知道如何实现存储过程,以便任何时候新记录inserted或当前记录updated记录存储在我的INV_AssetsHistory表中?

我的INV_Assets表定义为:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using GridMvc.DataAnnotations;
using System.Web.Mvc;
using InventoryTracker.Models;

namespace InventoryTracker.Models
{
    [GridTable(PagingEnabled = true, PageSize = 30)]
    public class INV_Assets 
    {
        // Setting GridColumn Annotations allows you to use AutoGenerateColumns on view to auto create the Grid based on the model.


        public int Id { get; set; }

        public int Model_Id { get; set; }
        [ForeignKey("Model_Id")]
        public virtual INV_Models Model { get; set; }

        [Required]
        public int Manufacturer_Id { get; set; }
        [ForeignKey("Manufacturer_Id")]
        public virtual INV_Manufacturers Manufacturer { get; set; }

        [Required]
        public int Type_Id { get; set; }
        [ForeignKey("Type_Id")]
        public virtual INV_Types Type { get; set; }

        [Required]
        public int Location_Id { get; set; }
        [ForeignKey("Location_Id")]
        public virtual INV_Locations Location { get; set; }

        public int Vendor_Id { get; set; }
        [ForeignKey("Vendor_Id")]
        public virtual INV_Vendors Vendor { get; set; }

        [Required]
        public int Status_Id { get; set; }
        [ForeignKey("Status_Id")]
        public virtual INV_Statuses Status { get; set; }

        public string ip_address { get; set; }

        public string mac_address { get; set; }

        [DataType(DataType.MultilineText)]
        public string note { get; set; }
        public string owner { get; set; }

        //[DataType(DataType.Currency)]
        //[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode=true)]
        [DisplayFormat(DataFormatString = "{0:#,###0.00}", ApplyFormatInEditMode=true)]
        public decimal cost { get; set; }
        public string po_number { get; set; }

        [DataType(DataType.MultilineText)]
        public string description { get; set; }

        public int invoice_number{ get; set; }

        [Required]
        public string serial_number { get; set; }

        [Required]
        public string asset_tag_number { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? acquired_date { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? disposed_date { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime? verified_date { get; set; }

        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime created_date { get; set; }

        [Required]
        public string created_by { get; set; }

        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
        public DateTime? modified_date { get; set; }

        public string modified_by { get; set; }

        // Flag to specify if item is available? (Not signed out, not auctioned, recycled, etc.)
        //public bool available { get; set; }
    }
}

我已经设置了INV_AssetsHistory表(我将其视为一个独立的表,没有将其与其他表相关联的键值;这与{{1}之间唯一的主要区别}是当前表的INV_Assets字段和指定资产的Id

AssetsId

我正在阅读THIS文章并试图实现我想要的功能。在我的using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using GridMvc.DataAnnotations; using System.Web.Mvc; using InventoryTracker.Models; namespace InventoryTracker.Models { public class INV_AssetsHistory { public int Id { get; set; } [Required] public int AssetId { get; set; } //public int Model_Id { get; set; } //[ForeignKey("Model_Id")] public virtual INV_Models Model { get; set; } [Required] //public int Manufacturer_Id { get; set; } //[ForeignKey("Manufacturer_Id")] public virtual INV_Manufacturers Manufacturer { get; set; } [Required] //public int Type_Id { get; set; } //[ForeignKey("Type_Id")] public virtual INV_Types Type { get; set; } [Required] //public int Location_Id { get; set; } //[ForeignKey("Location_Id")] public virtual INV_Locations Location { get; set; } //public int Vendor_Id { get; set; } //[ForeignKey("Vendor_Id")] public virtual INV_Vendors Vendor { get; set; } [Required] //public int Status_Id { get; set; } //[ForeignKey("Status_Id")] public virtual INV_Statuses Status { get; set; } public string ip_address { get; set; } public string mac_address { get; set; } [DataType(DataType.MultilineText)] public string note { get; set; } public string owner { get; set; } //[DataType(DataType.Currency)] //[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode=true)] [DisplayFormat(DataFormatString = "{0:#,###0.00}", ApplyFormatInEditMode = true)] public decimal cost { get; set; } public string po_number { get; set; } [DataType(DataType.MultilineText)] public string description { get; set; } public int invoice_number { get; set; } [Required] public string serial_number { get; set; } [Required] public string asset_tag_number { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime? acquired_date { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime? disposed_date { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime? verified_date { get; set; } [Required] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime created_date { get; set; } [Required] public string created_by { get; set; } [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")] public DateTime? modified_date { get; set; } public string modified_by { get; set; } } } 文件中,我有以下内容:

InventoryTrackerContext.cs

在关注本文后,我添加了这个(namespace InventoryTracker.DAL { public class InventoryTrackerContext : DbContext { public InventoryTrackerContext() : base("InventoryTrackerContext") { } public DbSet<INV_Assets> INV_Assets { get; set; } public DbSet<INV_Models> INV_Models { get;set; } public DbSet<INV_Manufacturers> INV_Manufacturers { get;set; } public DbSet<INV_Types> INV_Types { get; set; } public DbSet<INV_Locations> INV_Locations { get; set; } public DbSet<INV_Vendors> INV_Vendors { get; set; } public DbSet<INV_Statuses> INV_Statuses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<INV_Assets>().MapToStoredProcedures(); } )特定代码行并更新了我的上下文。现在在modelBuilder.Entity<INV_Assets>().MapToStoredProcedures(); Server Explorer连接中,我有3个名为InventoryTrackerContext的存储过程。例如,INV_Assets_Delete/Insert/Update定义为:

Update

根据这篇文章,我假设当我更新CREATE PROCEDURE [dbo].[INV_Assets_Update] @Id [int], @Model_Id [int], @Manufacturer_Id [int], @Type_Id [int], @Location_Id [int], @Vendor_Id [int], @Status_Id [int], @ip_address [nvarchar](max), @mac_address [nvarchar](max), @note [nvarchar](max), @owner [nvarchar](max), @cost [decimal](18, 2), @po_number [nvarchar](max), @description [nvarchar](max), @invoice_number [int], @serial_number [nvarchar](max), @asset_tag_number [nvarchar](max), @acquired_date [datetime], @disposed_date [datetime], @verified_date [datetime], @created_date [datetime], @created_by [nvarchar](max), @modified_date [datetime], @modified_by [nvarchar](max) AS BEGIN UPDATE [dbo].[INV_Assets] SET [Model_Id] = @Model_Id, [Manufacturer_Id] = @Manufacturer_Id, [Type_Id] = @Type_Id, [Location_Id] = @Location_Id, [Vendor_Id] = @Vendor_Id, [Status_Id] = @Status_Id, [ip_address] = @ip_address, [mac_address] = @mac_address, [note] = @note, [owner] = @owner, [cost] = @cost, [po_number] = @po_number, [description] = @description, [invoice_number] = @invoice_number, [serial_number] = @serial_number, [asset_tag_number] = @asset_tag_number, [acquired_date] = @acquired_date, [disposed_date] = @disposed_date, [verified_date] = @verified_date, [created_date] = @created_date, [created_by] = @created_by, [modified_date] = @modified_date, [modified_by] = @modified_by WHERE ([Id] = @Id) END 记录时,此程序会自动触发。如果是的话,它并没有像我想要的那样在我的INV_Asset表中添加记录 - 这看起来似乎不是指示的SQL。

我是否需要手动修改我的INV_AssetsHistory存储过程,或者是否有一些简单的代码我忽略/不知道要获得我想要完成的内容?有没有更有经验的护理来衡量?

我目前正在使用本地DBContext,但在INV_Assets_Update中,这些表将位于ORACLE数据库中。

1 个答案:

答案 0 :(得分:1)

正如其他人已经评论过的那样,使用触发器,这是一个例子:

CREATE TRIGGER INV_ASSET_BRIU ON INV_Asset
BEFORE INSERT OR UPDATE
FOR EACH ROW IS
BEGIN
    INSERT INTO INV_AssetHistory(id, col1, col2, ..., action_timestamp)
    VALUES (:NEW.id, :NEW.col1, :NEW.col2, ..., SYSTIMESTAMP);
END;

使用此触发器将每个版本插入到表INV_AssetHistory