我正在開發一個MVC5/EF代碼優先應用程序。有誰知道如何實現存儲過程,以便在新記錄是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
表我已經設置爲這樣(我將此視爲與它鏈接到其他表沒有鍵值一個獨立的表,唯一的這與INV_Assets
之間的主要區別是當前表Id
場和AssetsId
指定資產):
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; }
}
}
我讀了THIS文章並試圖實現我所需的功能。在我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
爲InventoryTrackerContext
連接,我有3個存儲過程,標題爲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
記錄此過程中會自動閃光。如果是這樣,它不會像我打算的那樣向我的INV_AssetsHistory
表添加記錄 - 這看起來不像SQL所指示的那樣。
是否需要手動修改我的INV_Assets_Update
存儲過程,還是有幾行簡單的代碼我忽略/不知道要獲得我想要完成的內容?任何更有經驗的護理人員可以稱重?
我目前使用的是本地DBContext,但是在PROD
這些表將會在ORACLE數據庫中。
上INV_ASSET觸發器會很快做的工作簡單地說。 – kevinsky 2015-03-13 19:26:15
我不確定EF是否是這裏最好的選擇,看起來像是數據庫觸發器的完美場景。 – 2015-03-13 19:28:06
你們中的任何一個人都可以提供一個示例或鏈接來指導如何設置?我對Sequel Server/Management Studio比較熟悉,但是ORACLE和Toad是我的新技術/工具。 – 2015-03-13 20:39:08