我有一张具有复合主键的表格,如下所示。我正在尝试使用C#MVC添加/更新/删除功能。添加和删除工作正常但EffectiveDate列的更新失败,因为存在多个具有相同ClientName和Portfolio的行。表结构和实体/服务代码如下。你能看看我在代码中遗漏了什么吗?
运行时错误:
存储更新,插入或删除语句会影响意外 行数(0)。自那以后,实体可能已被修改或删除 实体已加载。看到 http://go.microsoft.com/fwlink/?LinkId=472540了解有关的信息 理解和处理乐观并发异常。
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)
我认为您使用RiskReportDataViewModel进行了更新,您是否使用自定义更新? 告诉我更新的方法。
实际上,你需要选择获得&#34;自定义&#34;首先从数据库中更改&#34; Custom&#34;并保存。
以及为什么自定义模型中有三个键如下所示?
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; }
}
为key添加一个整数值。
答案 1 :(得分: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]
服务代码
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();
}