1

I was trying to insert a values through EF. But I'm facing a issue while inserting, I know that my table don't have any primary key and also i am passing all values so that to avoid not null column errors. So I can't able to predict why its not inserting. Below is my code,

foreach (var obj in newEntities)
{
    context.XXX.Add(obj);
    context.Entry(obj).State = EntityState.Added;
    context.SaveChanges();
}



[System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Gene‌​rator", "2.33.0.0")] 
public partial class Example 
{ 
  public System.DateTime DateOff { get; set; } // DateOff (Primary key)
  public short Num { get; set; } // Num (Primary key) 
  public System.DateTime DateAdded { get; set; } // DateAdded 
  public int Id { get; set; } // Id 
  public string CCode { get; set; } // CCode 
  public string Comments { get; set; } // Comments 
}

Kindly suggest me to get rid from this issue. Thanks.

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
Sabarish
  • 59
  • 1
  • 6
  • Adding a primary key would be a good start and might even solve this problem. Is there a specific reason this table doesn't have a PK? – Mixxiphoid Oct 31 '17 at 11:20
  • without PK, I don't think you can get rid of this problem. EF checks after SaveChanges() for concurrency. For this it takes concurrency tokens from saved entries and looks into the database if the object with the given values is in the database. a standard concurrency token is the PK - and there will never be any object with a PK value of null in the database. – DevilSuichiro Oct 31 '17 at 11:20
  • @Mixxiphoid There is no any specific reason. But is there no other way without adding PK? – Sabarish Oct 31 '17 at 12:30
  • @DevilSuichiro thanks for the comment and agree with you. But I tried to do update and delete it works fine for me. That's why i'm wondering why its impossible for Insert? – Sabarish Oct 31 '17 at 12:32
  • I don't think EF6 works without a PK. See [here](https://stackoverflow.com/questions/3996782/entity-framework-table-without-primary-key). The table could have a PK by convention. Show the model. – Steve Greene Oct 31 '17 at 19:00
  • @SteveGreene This is was the model generated by POCO tool [System.CodeDom.Compiler.GeneratedCode("EF.Reverse.POCO.Generator", "2.33.0.0")] public partial class Example { public System.DateTime DateOff { get; set; } // DateOff (Primary key) public short Num { get; set; } // Num (Primary key) public System.DateTime DateAdded { get; set; } // DateAdded public int Id { get; set; } // Id public string CCode { get; set; } // CCode public string Comments { get; set; } // Comments } – Sabarish Nov 01 '17 at 11:07
  • Always edit your question to add code. So if that is your model and you have added no fluent configuration, the the field "Id" is your primary key due to EF's conventions - See [here](https://msdn.microsoft.com/en-us/data/jj679962). DateOff and Num are not primary keys unless you add annotations or fluent code. – Steve Greene Nov 01 '17 at 13:06

0 Answers0