2017-01-24 47 views
0

我有一個表具有複合主鍵,如下所示。我試圖使用C#MVC添加/更新/刪除功能。添加和刪​​除工作正常,但更新EffectiveDate列失敗,因爲存在多個具有相同ClientName和Portfolio的行。表格結構和實體/服務代碼在下面提供。你能看看我在代碼中缺少什麼嗎?C#實體更新表失敗

運行時錯誤:

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

CREATE TABLE [dbo].[CustomAUM](
[Client] [varchar](80) NOT NULL, 
[Portfolio] [varchar](100) NOT NULL, 
[AUM] [numeric](30, 6) NOT NULL, 
[EffectiveDate] [datetime] NOT NULL, 
[IsStatic] [char](1) NULL, 
[sysDate] [datetime] NOT NULL, 
[ModifiedBy] [varchar](80) NOT NULL, 
CONSTRAINT [PK_CustomAUM] PRIMARY KEY CLUSTERED 
(
    [Portfolio] ASC, 
    [Client] ASC, 
    [EffectiveDate] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] 
) ON [PRIMARY] 

實體代碼

public class Custom 
{ 

    [Key] 
    [Column(Order = 1)] 
    public virtual string Client { get; set; } 

    [Key] 
    [Column(Order = 2)] 
    public virtual string Portfolio { get; set; } 

    [Required] 
    public virtual decimal AUM { get; set; } 

    [Key] 
    [Column(Order = 3)] 
    public DateTime EffectiveDate { get; set; } 

    [StringLength(50)] 
    [Column(TypeName = "char")] 
    public string IsStatic { get; set; } 

    [Required] 
    public DateTime sysDate { get; set; } 

    [StringLength(500)] 
    public virtual string ModifiedBy { get; set; } 

} 

public class CustomerMap : EntityTypeConfiguration<Custom> 
{ 
    public CustomerAUMMap() 
    { 
     //Primary Key 
     this.HasKey(k => new { k.Client, k.Portfolio, k.EffectiveDate }); 

     this.ToTable("CustomAUM"); 

     this.Property(x => x.Client).HasColumnName("Client"); 
     this.Property(x => x.Portfolio).HasColumnName("Portfolio"); 
     this.Property(x => x.AUM).HasColumnName("AUM"); 
     this.Property(x => x.EffectiveDate).HasColumnName("EffectiveDate"); 
     this.Property(x => x.IsStatic).HasColumnName("IsStatic"); 
     this.Property(x => x.sysDate).HasColumnName("sysDate"); 
     this.Property(x => x.ModifiedBy).HasColumnName("ModifiedBy"); 
    } 
} 

服務方法

public void CustomAUM_Update(RiskReportDataViewModel riskReportDataViewModel, string userName) 
    { 
      var entity = new Custom(); 

      entity.Client = riskReportDataViewModel.Client; 
      entity.Portfolio = riskReportDataViewModel.Portfolio; 
      entity.AUM = riskReportDataViewModel.AUM; 
      entity.EffectiveDate = DateTime.Parse(riskReportDataViewModel.EffectiveDate.ToShortDateString()); 
      entity.IsStatic = riskReportDataViewModel.IsStatic; 
      entity.sysDate = DateTime.Now; 
      entity.ModifiedBy = userName; 

      riskContext.Custom.Attach(entity); 
      riskContext.Entry(entity).State = EntityState.Modified; 
      riskContext.SaveChanges(); 
    } 

//視圖模型

public class RiskReportDataViewModel 
{ 

    [Key] 
    public virtual string Client { get; set; } 

    [Key] 
    [StringLength(500)] 
    public virtual string Portfolio { get; set; } 

    public virtual decimal AUM { get; set; } 

    [Key] 
    public virtual DateTime EffectiveDate { get; set; } 

    public virtual String IsStatic { get; set; } 

    public virtual DateTime sysDate { get; set; } 

    [StringLength(500)] 
    public virtual string ModifiedBy { get; set; } 

} 
+0

什麼錯誤? –

+0

我已更新RunTime我收到錯誤。我認爲這意味着無法保存數據庫中的任何行。 - 謝謝。 – Partha

回答

0

解決此問題的方法是添加一個新的行作爲ID並創建一個組合主鍵。感謝Jun An分享想法。修改後的代碼看起來是這樣的 -

數據庫表

CREATE TABLE [dbo].[CustomAUM](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [Client] [varchar](80) NOT NULL, 
    [Portfolio] [varchar](100) NOT NULL, 
    [AUM] [numeric](30, 6) NOT NULL, 
    [EffectiveDate] [datetime] NOT NULL, 
    [IsStatic] [char](1) NULL, 
    [sysDate] [datetime] NOT NULL, 
    [ModifiedBy] [varchar](80) NOT NULL, 
CONSTRAINT [PK_CustomAUM_New] PRIMARY KEY CLUSTERED 
(
    [ID] ASC, 
    [Portfolio] ASC, 
    [Client] ASC, 
    [EffectiveDate] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] 
) ON [PRIMARY] 

Service Code

public void CustomAUM_Update(RiskReportDataViewModel riskReportDataViewModel, string userName) 
     { 
       var entity = new CustomAUM(); 

       entity = riskContext.CustomAUM.FirstOrDefault(x => x.ID == riskReportDataViewModel.ID && x.Client == riskReportDataViewModel.Client && x.Portfolio == riskReportDataViewModel.Portfolio); 

       entity.Client = riskReportDataViewModel.Client; 
       entity.Portfolio = riskReportDataViewModel.Portfolio; 
       entity.AUM = riskReportDataViewModel.AUM; 
       entity.EffectiveDate = DateTime.Parse(riskReportDataViewModel.EffectiveDate.ToShortDateString()); 
       entity.IsStatic = riskReportDataViewModel.IsStatic; 
       entity.sysDate = DateTime.Now; 
       entity.ModifiedBy = userName; 

       riskContext.CustomAUM.Attach(entity); 
       riskContext.Entry(entity).State = EntityState.Modified; 
       riskContext.SaveChanges(); 
     } 
0

我想你RiskReportDataViewModel更新,並使用自定義更新? 告訴我更新的方法。

實際上,您需要先從數據庫中選擇「自定義」,然後更改「自定義」的值並保存。

以及爲什麼你在自定義模型中有三個按鍵,如下所示?

public class Custom 
{ 

[Key] 
[Column(Order = 1)] 
public virtual string Client { get; set; } 

[Key] 
[Column(Order = 2)] 
public virtual string Portfolio { get; set; } 

[Required] 
public virtual decimal AUM { get; set; } 

[Key] 
[Column(Order = 3)] 
public DateTime EffectiveDate { get; set; } 

[StringLength(50)] 
[Column(TypeName = "char")] 
public string IsStatic { get; set; } 

[Required] 
public DateTime sysDate { get; set; } 

[StringLength(500)] 
public virtual string ModifiedBy { get; set; } 

} 

爲密鑰添加一個整數值。

+0

我提供了CustomAUM_Update方法代碼。我也嘗試刪除兩個鍵。我認爲該表具有複合主鍵,所以我認爲使用三個鍵。當我刪除兩個鍵時,這是我得到的錯誤。違反PRIMARY KEY約束'PK_CustomAUM'。無法在對象'dbo.CustomAUM'中插入重複鍵。重複的鍵值是(L/S_BMAT(STEVENS),Verition,Jan 2 2017 12:00 AM)。 該語句已終止。 – Partha

+0

向我顯示更新的代碼方法 –

+0

服務代碼隨CustomAUM_Update方法的更新代碼一起提供。 - 謝謝。 – Partha