asp.net - EF : Cannot insert explicit value for identity column in table 'Groups' when IDENTITY_INSERT is set to OFF

标签 asp.net sql entity-framework c#-4.0 identity-insert

是的,这是另一个问题。我已经看过这里但无济于事..
这就是我的情况。
我创建了一个 ASP 站点,最初使用的是 SQLce。事实证明这很糟糕,因为主机不支持它。我通过Web Matrix方法将sdf文件转换为SQL server 2012 db。数据库一切都好,一切都可以读取数据。当我尝试向表中写入任何内容时,问题就出现了。 VS 出现说:
当 IDENTITY_INSERT 设置为 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,还有什么方法?

最佳答案

转到您的数据模型并验证身份列的“StoreGeneratePattern”属性是否已有效设置为“Identity”。

关于asp.net - EF : Cannot insert explicit value for identity column in table 'Groups' when IDENTITY_INSERT is set to OFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13509112/

相关文章:

mysql - 当我插入记录时关系表不更新

c# - 从 dbcontext 中的方法返回通用表

c# - 如何从内容页面的代码后面更改母版页的背景?

jquery - 使用 jquery 隐藏和显示表格行元素

asp.net - 使用 asp.net 触发 javascript 事件

mysql - 如何防止表中的重复记录插入忽略在这里不起作用

asp.net - 如何在 .csproj 文件(而不是 project.json)中指定 ASP.NET Core 目标框架导入?

sql - "Must declare the scalar variable"执行存储过程时出错

.net - 每个开发人员都应该知道的ADO.NET Entity Framework 性能提示

c# - 动态创建 Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>?