У меня есть таблица с составным первичным ключом, как показано ниже. Я пытаюсь добавить/обновить/удалить функциональность с помощью C# MVC. Add и Delete отлично работают, но Update for EffectiveDate Column не работает, поскольку существует несколько строк с тем же именем ClientName и Portfolio. Код таблицы и код объекта/службы приведены ниже. Не могли бы вы посмотреть, что мне не хватает в коде?CUE Обновление таблицы сущностей
RunTime Error:
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]
Entity Код
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");
}
}
Метод Service
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