I can't save data using Entity Framework
Asked Answered
T

0

2

I use this code to add one item to a SpareParts table in my database.

try
{
    Database1Entities _db = new Database1Entities();

    SparePart sparePart = new SparePart();
    sparePart.manufactureID = 2;
    sparePart.name = "detail";

    _db.SpareParts.AddObject(sparePart);

    int changesNumber = _db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

    int count = _db.SpareParts.Count();
    int i = 8 + 1;
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

I tried _db.SaveChanges(). That didn't help.

I don't get any exceptions. And if I start debug 4 times, I will see count = 4. And I can iterate through all my items. But if I wait 1 minute and start debugging one more time, I see count = 1. So it seems that my changes are still not in database.

Trysail answered 14/11, 2012 at 15:24 Comment(12)
have you tried stepping thru w/ a debuggerDuplication
start sql profiler and see if any insert statement is coming.Monovalent
of course, and count = 4 after 4 times of starting debugging with 5 second time interval between debuggingTrysail
Giedrius, how to start sql profilerTrysail
Have you read this SO article? #3920611Amain
I didn't find help in this articleTrysail
And you're not doing anything different between the 4th and 5th run through? No cleaning and/or rebuilding?Stiffler
Are you sure something else isn't editing the database? Have you tried just watching the data in there directly (such as via repeated Select statements)?Ague
Can you show us your connection string?Dorotheadorothee
First, I do this in console application. Connection string is Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=TrueTrysail
Refer to the answer (https://mcmap.net/q/1371415/-how-to-add-entity-framework-to-console-application-images-are-included) I just gave you on your other SO question.Ytterbium
possible duplicate of How to add entity-framework to console application (images are included)Christinchristina

© 2022 - 2024 — McMap. All rights reserved.