开发者

Entity Framework does not save object

开发者 https://www.devze.com 2023-01-29 14:12 出处:网络
Entity Framework does not save object. Nex开发者_开发百科t code runs without any error, but changes are not affected in DB.

Entity Framework does not save object. Nex开发者_开发百科t 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.


Where is your Db?

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


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();


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();         
0

精彩评论

暂无评论...
验证码 换一张
取 消