是的,又是这种问题。我在这里找过,但没有结果。我的情况是这样的。我开始使用SQLce创建了一个ASP网站。但是,由于主机不支持它,这证明是不好的。我通过Web Matrix方法将sdf文件转换为SQL Server 2012数据库。数据库很好,一切都可以读取数据。但当我尝试向表中写入任何内容时,问题就来了。VS会出现以下提示:Cannot insert explicit value for identity column in table 'Groups' when IDENTITY_INSERT is set to OFF. 现在,在此之前,一切都运行良好。这是我设置的一个表的示例:
Column1: ID - Int - Primary key. - Identity specification yes, with a increment of 1
Column2: GroupName - nvarchar(100) - just a normal text field
那么,它在哪里卡住了呢?代码在哪里出问题了:
using (SLEntities context = new SLEntities())
{
var namecheck = (from n in context.Groups
where n.GroupName == newname
select n).Count();
if (namecheck > 0)
{
// the name already exists.. return
lab_Error.Text = newname + " is already in the database. Please use another name";
return;
}
// namecheck is new.. add to db
Group g = new Group();
g.GroupName = newname;
context.Groups.Add(g);
context.SaveChanges();
}
在savechanges()时,程序出现错误。
我的问题是如何修复它?(我看到了IDENTITY_INSERT ON的方法,但那不是只在创建表时使用吗?)或者,除了使用自动增量ID之外,还有什么其他方法可以解决这个问题?