2009-09-18 88 views
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

待辦事項你有什麼線索可能會發生什麼?

在此先感謝!

+0

設置新值在此之前排隊什麼的「Directivo」級是什麼樣子? – Charlino 2009-09-18 02:26:46

+0

我使用這些信息更新了我的問題,謝謝! – 2009-09-18 11:12:02

+0

對不起,但是......如果它只發生在一個類上,我會建議將它與其他Linq2SQL類進行比較,看看有什麼不同。我還會通過調試器跟蹤兩個不同的Linq2SQL實體,以查看不同階段的差異。除此之外,我無法真正幫助對不起。 – Charlino 2009-09-21 21:21:55

回答

0

我認爲這個問題是在this._table.Attach(entity);舉措如下

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 
    { 
     this._table.Attach(entity); 

     entity.UsuarioModificacion = userName; 
     entity.FechaModificacion = DateTime.Now; 

     this._table.Context.Refresh(RefreshMode.KeepCurrentValues, entity); 
    } 
    try 
    { 
     this._dataContext.SubmitChanges(); 
    } 
    catch (SqlException ex) 
    { 
     throw new DataContextException(ex); 
    } 

} 

,這可能會幫助你http://www.richardbushnell.net/2008/02/18/how-to-update-data-with-linq-to-sql/