且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

{“当IDENTITY_INSERT设置为OFF时,无法在表”Cantact“中插入标识列的显式值。”}

更新时间:2023-01-22 09:06:14

我需要更新我的我的模型中的.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