Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizPlease log in to access this content. You will be redirected to the login page shortly.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
The ID of the newly created record doesn't come from the SaveChanges call. SaveChanges will update all modified/inserted/deleted records.
Provided you have configured your entity as recognizing it's ID is DatabaseGeneratedOption.Identity then to get the newly inserted ID:
stodoc.StockOutDocument_Serial = srlnum;
stodoc.StockOutDocument_date = inview.InventoryDate;
db.StockOutDocuments.Add(stodoc);
db.SaveChanges();
int stodocid = stodoc.stodocid; // Will be populated once SaveChanges is called.
If the stodoc.stodocid property is not updated, check that you have configured it in the mapping as an Identity column:
If you used attributes in the entity:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int stodocid { get; set; }
or via entity configuration: EF Core:
builder.HasKey(x => x.stodocid);
builder.Property(x => x.stodocid).UseSqlServerIdentityColumn();
EF6:
HasKey(x => x.stodocid)
.Property(x => x.stodocid)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Please log in to access this content. You will be redirected to the login page shortly.
Login
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
I'm using EF and ASP.NET MVC 5, and when inserting into database using
the returned
idis always "1", however in the SQL database, the inserted record'sidis "3" (before inserting I deleted the first two records in the database table to clear the table).Could anyone tell me how to fix this? I feel EF is not update to date with database or am missing any settings to sync both? Many thanks in advance...
My code below...