Anton
Anton

Reputation: 9961

Entity Framework does not save object

Entity Framework does not save object. Next code runs without any error, but changes are not affected in DB.

using (MedDbEntities me = new MedDbEntities())
            {
                Patients p = new Patients();
                p.lastName = _uc.lastNameTextBox.Text;
                p.firstName = _uc.firstNameTextBox.Text;
                p.middleName = _uc.middleNameTextBox.Text;
                p.sex = 0;
                if (_uc.sexComboBox.SelectedText.Equals("ч"))
                    p.sex = 1;
                if (_uc.sexComboBox.SelectedText.Equals("ж"))
                    p.sex = 2;
                p.birthday = _uc.birthdayDateTimePicker.Value;

                me.AddToPatients(p);
                me.SaveChanges();
            }

Please, advise where can be problem.

Upvotes: 2

Views: 2661

Answers (3)

Rob
Rob

Reputation: 10248

You may need to use the ObjectStateManager to change the state of the object to Updated because it is a newly instantiated entity

MyEntities db = new MyEntities();

Product product = new Product();
product.Title = "My New Product";

db.AddToProduct(product);
db.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Updated);
db.SaveChanges();         

Upvotes: 0

Naz
Naz

Reputation: 5144

Maybe you have to do SaveChanges inside of AddToPatients method ? Inside of that method you sould have something like this :

_db.Patients.AddObject(patient);
_db.SaveChanges();

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273169

Where is your Db?

An attached-file database is copied (overwritten) on each build.

Upvotes: 2

Related Questions