0
我使用asp.net mvc与linq到sql存储库,下面的代码在this._table上抛出一个mvc System.Data.Linq.DuplicateKeyException异常。附加(实体)asp.net mvc System.Data.Linq.DuplicateKeyException更新
我的代码是类似的东西:
public ActionResult Edit(int id)
{
return View(_controllerRepository.GetById(id));
}
public ActionResult Edit(Directivo entity)
{
try
{
_repository.Save(entity, this.UserName)
}
catch (Exception ex)
{
return View(ex);
}
}
,在库中:
public virtual void Save(T entity, string userName)
{
if (0 == entity.Id)
{
entity.UsuarioIntroduccion = userName;
entity.FechaIntroduccion = DateTime.Now;
entity.UsuarioModificacion = null;
entity.FechaModificacion = null;
this._table.InsertOnSubmit(entity);
}
else
{
entity.UsuarioModificacion = userName;
entity.FechaModificacion = DateTime.Now;
this._table.Attach(entity);
this._table.Context.Refresh(RefreshMode.KeepCurrentValues, entity);
}
try
{
this._dataContext.SubmitChanges();
}
catch (SqlException ex)
{
throw new DataContextException(ex);
}
}
注意标识符不为0
它真的很奇怪,因为它只发生在这个班上,我还有一些工作得很好。
的表是:
CREATE TABLE [Directivo](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Nombre] [varchar](45) NOT NULL,
[Apellidos] [varchar](60) NOT NULL,
[FechaNacimiento] [datetime] NULL,
[CargoDirectivoId] [int] NOT NULL,
[PathImagen] [varchar](250) NULL,
FechaIntroduccion datetime not null,
UsuarioIntroduccion varchar(45) not null,
FechaModificacion datetime,
UsuarioModificacion varchar(45),
PRIMARY KEY (Id),
FOREIGN KEY (CargoDirectivoId)
REFERENCES CargoDirectivo(Id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
和类是自动生成的通过LINQ和部分类,使得它继承的接口,并设置好友类的元数据使用XVAL
待办事项你有什么线索可能会发生什么?
在此先感谢!
设置新值在此之前排队什么的“Directivo”级是什么样子? – Charlino 2009-09-18 02:26:46
我使用这些信息更新了我的问题,谢谢! – 2009-09-18 11:12:02
对不起,但是......如果它只发生在一个类上,我会建议将它与其他Linq2SQL类进行比较,看看有什么不同。我还会通过调试器跟踪两个不同的Linq2SQL实体,以查看不同阶段的差异。除此之外,我无法真正帮助对不起。 – Charlino 2009-09-21 21:21:55