C# 实体更新 Table 失败
C# Entity Update Table Failing
我有一个 table,它具有如下所示的复合主键。我正在尝试 add/update/delete 使用 C# MVC 的功能。添加和删除工作正常,但 EffectiveDate 列的更新失败,因为存在具有相同 ClientName 和 Portfolio 的多行。 Table结构和Entity/Service代码如下。你能看看我在代码中遗漏了什么吗?
运行时错误:
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; }
}
我想你是用 RiskReportDataViewModel 更新的,你是用 Custom 更新的吗?
告诉我更新的方法。
实际上,您需要先select从数据库中获取"Custom",然后更改"Custom"的值并保存。
为什么自定义模型中有如下三个键?
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; }
}
为键添加一个整数值。
解决这个问题的方法是添加一个新的Row作为ID,并做一个复合主键。感谢 Jun An 分享想法。修改后的代码如下所示 -
DB Table
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();
}
我有一个 table,它具有如下所示的复合主键。我正在尝试 add/update/delete 使用 C# MVC 的功能。添加和删除工作正常,但 EffectiveDate 列的更新失败,因为存在具有相同 ClientName 和 Portfolio 的多行。 Table结构和Entity/Service代码如下。你能看看我在代码中遗漏了什么吗?
运行时错误:
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; }
}
我想你是用 RiskReportDataViewModel 更新的,你是用 Custom 更新的吗? 告诉我更新的方法。
实际上,您需要先select从数据库中获取"Custom",然后更改"Custom"的值并保存。
为什么自定义模型中有如下三个键?
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; }
}
为键添加一个整数值。
解决这个问题的方法是添加一个新的Row作为ID,并做一个复合主键。感谢 Jun An 分享想法。修改后的代码如下所示 -
DB Table
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();
}