2013-10-09 33 views
1

我正在使用ServiceStack ORMLite嘗試更新數據庫中的記錄。我所有的POCO的實施IHasID接口ServiceStack ORMLite無法更新標識列

public interface IHasId 
{ 
    int Id { get; set; } 
} 

在我的POCO我有以下屬性

private int id; 

[ServiceStack.DataAnnotations.Alias("TableName_ID")] 
    public int Id 
    { 
     get { return id; } 
     set 
     { 
      if (value != this.id) 
      { 
       this.id = value;      
       NotifyPropertyChanged("Id"); 
      } 
     } 
    } 

我的每一個庫類從DataRepositoryBase <牛逼繼承>類具有下列方法:

public virtual T Update(T entity) 
    { 
     using (IDbConnection db = CreateDbConnection()) 
     { 
      db.Update<T>(entity, e => e.Id == entity.Id); 
      return entity; 
     } 
    } 

當我呼籲孩子庫我收到以下錯誤此方法:

System.Data.SqlClient.SqlException (0x80131904): Cannot update identity column 'TableName_ID'.

我該如何解決這個錯誤?

我在做什麼錯?

謝謝!

傑里米

回答

3

你也應該標註標識列與[AutoIncrement],e.g:

[AutoIncrement] 
[Alias("TableName_ID")] 
public int Id { ... ] 
+0

謝謝你的工作! – jkruer01

+0

另外,如果不添加[AutoIncrement]數據註釋,我將更新方法更改爲:db.Update (entity);而且也有效。謝謝! – jkruer01

0

你通常不會因爲它們是由數據庫自動生成更新標識字段。在某些情況下,您可能想要更新標識字段,但是您需要使用SQL來完成此操作,因爲它需要您在每個查詢結束時使用SET語句。