Questo è il mio codice, molto semplice ...codice prima - Impossibile inserire il valore NULL nella colonna 'Id'
var newUser = new User();
newUser.Id=id;
newUser.Email = email;
this.DataContext.Set<User>().Add(newUser);
this.DataContext.SaveChanges();
L'errore che ottengo è una SQLException in this.DataContext.SaveChanges();
affermando che:
Impossibile inserire il valore NULL nella colonna "Id", tabella "xxxxx.dbo.Users"; la colonna non consente null. INSERT fallisce.
ho debug e ha scoperto che c'è un valore in Id & e-mail nel newUser a this.DataContext.Set<User>().Add(newUser);
Se questo è il caso, come viene il valore diventando nulla?
L'analisi dello stack di errore:
[DbUpdateException: An error occurred while updating the entries. See the inner exception for details.]
System.Data.Entity.Internal.InternalContext.SaveChanges() +204
System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +23
System.Data.Entity.DbContext.SaveChanges() +20
non sono stato in grado di comprendere o risolvere questo ....
Sarebbe sinceramente apprezzare alcun aiuto in questo ...
Saluti Arnab
Soluzione
Ok, grazie a Ladislav per indicarmi la giusta direzione: Aggiunta l'attributo [DatabaseGenerated(DatabaseGeneratedOption.None)]
risolto il problema.
Qual è il tipo di ID? Potrebbe essere che qualcosa stia andando storto nel generare la chiave nel database? –
type è bigint in sqlserver, in User class è lungo .. Anche quando utilizzo il normale framework di entità senza codefirst, funziona anche – Arnab
Il parametro id è anche lungo – Arnab