ServiceStack ORMLite 无法更新标识列 [英] ServiceStack ORMLite Cannot Update Identity Column

查看:39
本文介绍了ServiceStack ORMLite 无法更新标识列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ServiceStack ORMLite 尝试更新我数据库中的记录.我所有的 POCO 都实现了 IHasID 接口

I am using ServiceStack ORMLite to try and update a record in my database. All of my POCO's implement an IHasID interface

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

在我的 POCO 中,我有以下属性

In my POCO I have the following property

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 继承了一个

T> 具有以下方法的类:

Each of my repository classes inherits a from a DataRepositoryBase< T> class that has the following method:

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

当我在子存储库上调用此方法时,出现以下错误:

When I call this method on the child repository I get the following error:

System.Data.SqlClient.SqlException (0x80131904):无法更新标识列TableName_ID".

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

我该如何解决这个错误?

How can I fix this error?

我做错了什么?

谢谢!

杰瑞米

推荐答案

您还应该使用 [AutoIncrement] 标记标识列,例如:

You should also label Identity columns with [AutoIncrement], e.g:

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

这篇关于ServiceStack ORMLite 无法更新标识列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆