C# - Entity Framework add new object to ObjectContext
im working with Entity Framework, SQL and C#.
i have a Table called Client and other called clients_phone.
I have a form with a Xtragrid and using BindingSource I bind the IQueryable to the grid.
myBindingSource = new BindingSource();
myBindingSource.DataSource = clients; //Clients it is the IQueryable<Client>
myBindingSource.DataMember = "clients_phone";
myBindingSource.AllowNew = true;
Then, i wan to add a new clients_phone to my client. To do this, i make a New Client() and then add the Phone.
clients newclient = objContext.CreateObject<clients>();
newclient.clients_phone = newClients_Phone;
objCon开发者_如何转开发text.AddObject("Clients", newclient);
Finally i add the new clients_phone in the ObjectContext, but when i see the Xtrag clients_phone don't show.
Any idea of what happens??.
Thanks
Lucas B is right. Whenever you've gotten round to adding all the new Clients and Clients_phone's you need to call the SaveChanges() method in order to persist the data into the database.
Whether you need to create the client first and then do an update to add further client_phone entries or whatever. If you never call SaveChanges() method nothing you do will be saved back to the database.
Have you tried saving and commiting the object?
objContext.SaveChanges(true);
objContext.AcceptAllChanges();
I have done the SaveChanges() but I see it as a workaround.
Drawback of this: When the user cancels the action the new object will be in the DB.
Use case: "Create person"
- User select a menu item (or button or whatever) named "Create person" (and the new user should appear in a GridView control for example - SaveChanges() is called)
- The the user fills in First Name, Last name etc.
- But then the users discovers that he does not need to create that person (e.g. the user remembers that he has created that person already yesterday)
- The user does not press the Save button (menu item or whatever ;-)) he uses the "Cancel" menu item (or button or whatever) - and so there will be an orphan person in the DB
Other approach: In order to add the new person (not committed yet) to a GridView control you can set the DataSource to "current persons + new person"
private object GetBindingList(ObjectSet<Person> contextObjects, Person newObject)
{
List<Person> list = new List<Person>();
list.AddRange(contextObjects);
list.Add(newObject);
return list;
}
Usage:
PersonsBindingSource.DataSource = GetBindingList(Context.PersonSet, newPerson);
But this has a disadvantage: It works only the first time ... So you need to to something like:
PersonsBindingSource.DataSource = GetBindingList(Context.PersonSet, newPerson);
Context.PersonSet = Populate with persons from PersonsBindingSource.DataSource // ;-)
But why is contextObjects.AddObject(newObject); not working (the new item will not be displayed in the GridView - the problem only occurs for objects without foreign keys to other objects)
Also works only when calling SaveChanges():
PersonsBindingSource.DataSource = Context.PersonSet.Execute(MergeOption.AppendOnly);
精彩评论