我有一個表具有複合主鍵,如下所示。我試圖使用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; }
}
什麼錯誤? –
我已更新RunTime我收到錯誤。我認爲這意味着無法保存數據庫中的任何行。 - 謝謝。 – Partha