Hi
Maybe it's simple question, but 开发者_StackOverflow社区it's my first time with EF + win app so ... I'm building win app with EF by adding edmx as data source then drag and drop the tables to get navigator and binding source .. when I press the add button in the navigator it allows me to enter new data but when I save the context I get NULL data for all fields except the auto increment field ( the ID field ).... What should I do to save many entries ...? DO I have to loop over all the entities in the binding source and add them to the context ?? Thanx in advanceUpdate:
I forgot to say that when i use data grid view it adds the items correctly but with "Details" it didn't ..I figure it out ..
The problem appears when i drag and drop entity from data source as "Details" control ..
the problem was that there is no connection between the context and the new item in binding source ( which created by clicking the "Add" button in the navigator ) .. so we have to make this connection manually by getting the new entity and added it to the context every time we add new item .
Finally we should end the edit mode in the binding source and accept all changes after saving changes in the context ( accepting changes will allow us to update the new item after saving it ) .. and here is the code :
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
MyEntity newEnt = new MyEntity();
newEnt = (MyEntity)MyEntityBindingSource.Current;
ctx.AddToAuthors(newEnt);
}
private void MyEntityBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
try
{
MyEntityBindingSource.EndEdit();
ctx.SaveChanges();
ctx.AcceptAllChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I hope this was helpful..
精彩评论