I try to save a contact in my program, which is a simple phone book in C #, and I use linq and Entity Framework, and when I want to add or change any data, I get a runtime error
You cannot insert an explicit value for the identifier column in the Contact table if IDENTITY_INSERT is set to OFF.
Here is my code insert(add), on the other hand, I do not want to add any data to my primary key, which is the identifier, and I want to leave it on my SQL Server.
Thank you 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; }
}