{“当IDENTITY_INSERT设置为OFF时,无法在表”Cantact“中插入标识列的显式值。”} [英] {"Cannot insert explicit value for identity column in table 'Cantact' when IDENTITY_INSERT is set to OFF."}

查看:189
本文介绍了{“当IDENTITY_INSERT设置为OFF时,无法在表”Cantact“中插入标识列的显式值。”}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的程序中保存一个联系人,这是C#中的一个简单的电话簿,我正在使用linq&实体框架&当我想添加或更改任何数据时,我得到运行时错误


无法在表联系人中插入标识列的显式值IDENTITY_INSERT设置为OFF。


这是我的插入(添加)另一方面,我不想在我的主键中添加任何数据,这是ID,我想把它留给我的SQL Server。



感谢大家帮助我

  public void Save()
{
using(var Contex = new Phone_BookEntities1())
{
var p = from C in Contex.Cantacts
其中c.Cantact1 == Name
select new {c.Cantact1,c.Number};

if(!p.Any())
{
Ref_Cantact = new Cantact();
Ref_Cantact.Cantact1 = Name;
Ref_Cantact.Number = Num;
Contex.Cantacts.Add(Ref_Cantact);
Contex.SaveChanges();
}
}
}

strong>

  public partial class Cantact 
{
public string Cantact1 {get;组; }
public string Number {get;组; }
public int ID {get;组;
}


解决方案

我需要更新我的我的模型中的.edmx类


I'm trying to save a contact in my program which is a simple phone book in C# and I'm using linq & Entity Framework & when I want to add or change any data I get a run time error

Cannot insert explicit value for identity column in table 'Contact' when IDENTITY_INSERT is set to OFF.

Here's my insert (add) code, on the other hand I don't want to add any data in my primary key which is ID and I want to leave it to my SQL Server.

Thank you all for helping me

public void Save()
{
    using (var Contex = new Phone_BookEntities1())
    {
        var p = from c in Contex.Cantacts
                where c.Cantact1 == Name
                select new { c.Cantact1, c.Number };

        if (!p.Any())
        {
            Ref_Cantact = new Cantact();
            Ref_Cantact.Cantact1 = Name;
            Ref_Cantact.Number = Num;
            Contex.Cantacts.Add(Ref_Cantact);
            Contex.SaveChanges();
        }
    }
} 

EDIT

public partial class Cantact 
{ 
    public string Cantact1 { get; set; } 
    public string Number { get; set; } 
    public int ID { get; set; } 
} 

解决方案

I needed to update my .edmx class in my model

这篇关于{“当IDENTITY_INSERT设置为OFF时,无法在表”Cantact“中插入标识列的显式值。”}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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