“插入一条记录时,无法修改[列名= id]列

时间:2012-06-27 08:29:35

标签: windows-phone-7 sql-server-ce datacontext

我在Windows Phone 7平台上创建了一个数据库。表格中的一个定义如下。

[Table]
public class Playlist : BaseTable
{
    // Define ID: private field, public property, and database column.
    private int _id;

    [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = false, CanBeNull=false, IsPrimaryKey = true)]
    public int Id
    {
        get { return _id; }
        set
        {
            NotifyPropertyChanging("PlaylistId");
            _id = value;
            NotifyPropertyChanged("PlaylistId");
        }
    }
    // some other field
    //.......

}

我不希望字段“id”由db生成,所以“IsDbGenerated = false”,但插入一条记录时出现异常:       db.Playlists.InsertOnSubmit(新播放列表{Id =(int)DefalutPlaylist.Default,Name =“默认播放列表”,Group = 0,Type = 0});

它说“无法修改列[列名= id]”

谁能帮助我...

1 个答案:

答案 0 :(得分:4)

在DbType中删除“IDENTITY”值如下:

[Column(DbType = "INT NOT NULL", IsDbGenerated = false, CanBeNull=false, IsPrimaryKey = true)]

干杯