{"You cannot insert an explicit value for the identity column in the Cantact table if IDENTITY_INSERT is set to OFF." }

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; } 
} 
+4
3

.edmx

0

:

    public void Save(string Name, string Num)
    {
          using(var context =  new Phone_BookEntities1())
          {
               var existingContacts = Context.Cantacts.Where( c=>c.Cantact1 == Name); //there can be many contacts with the same name. Use FirstOrDefault and also improve the filtering   criteria
               if(existingContacts.Any())
               {
                    foreach(var contact in existingContacts)
                    {   
                         contact.Number = Num;
                    }
               }else
               {    
                      var Ref_Cantact =  new Cantact(){Cantact1 = Name, Number = Num};
                     context.Cantacts.Add(Ref_Cantact);
               }
               Contex.SaveChanges();
         }
    }
0

you can try this: this will wrap all the calls in the transaction, therefore setting the identity insert for the insert statement (created by EF when Add + SaveChanges was called).

if (!p.Any())
            {
                Ref_Cantact = new Cantact();
                Ref_Cantact.Cantact1 = Name;
                Ref_Cantact.Number = Num;
                using(var trans=Contex.Database.BeginTransaction())
                {
                    Contex.Database.ExecuteSqlStatement("SET IDENTITY_INSERT Contact ON;");
                    Contex.Cantacts.Add(Ref_Cantact);
                    Contex.SaveChanges();
                    trans.Commit();
                }
            }

EDIT: Another possibility is to disable AutoIncrement (DatabaseGeneratedOption.Identity) using (in your model constructor in a context class (or anyway)):

modelBuilder.Entity<Cantacts>().Property(x=>x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
0
source

Source: https://habr.com/ru/post/1615358/


All Articles