This is a problem that I couldn't find any trivial solutions besides that it is so common. I think the question is abstract enough to apply to both WPF and Winforms
Do you allow inserting childs if the master is still not saved?
This forces us to have an in-memory collection of items and to save the master and then the childs when the user hits the save button
What happens when deleting a child from an existing master?
If the user enter to a window for editing a master entity that have some childs, the deletion of a child should be done immediately or it should have an in-memory collection of deletes that its replicated when the user hits the save buttons.
This further complicates the problem because the program should manage the case when the user add a new child and then delete it, as this child doesn't exist on the database it shouldn't be added to the deletes list
What happens if a child entity is have its own children that can be edited?
If a master-detail view allow to edit the detail entities, and the edit window for this entities is another master-detail view, the save button if the inner child alters the database or somehow the save is delayed until the master save button is clicked
Do you insert a dummy empty master to make the logic easier?
I think is easier to insert an empty master entity when first creating one and that all CRUD operation over the master children affect directly the database on the newly created entity and if the cancel button is hitted, just delete the master and all its children. Does anyone have already done this?
The problem that I see here is that some tables have required values or foreign keys that can't just be assigned with a default value
This would be solved if the program didn't allow the user to add children entities until the user have saved the header data of the master detail window
Related
When trying to add record in child view, it require to save the parent record.
how can i stop this behavior?
Look into the photo
It is impossible to make it work unless you set LinkNewObjectToParentImmediately to false under Program.cs . But in this case there won't be any relationship between objects. You can override action by adding new controller but it is also useless in your case because there must an existing customer to link new invoice to.
I have the table A and Table B:
TableA(IDTableA,...) TableB(IDTableB, IDTableA,...)
I know that I can delete on cascada if I set this on the database, but if I haven't defined this in the database and I want to delete the children with EF, which is the best way to do that?
Imagine this situation: user A add to the context the parent, user A add the childs to context, mark the parent and the children as deleted and save the changes. But if an user B add a new child between the time user A loads the children and confirm the changes, the new child added by the user B is not deleted, or I get an exception because of reference integrity.
I try to use a transaction, to set the parent as deleted, then, in the next step, when I make the savechanges, the parent register is locked, so can't be load by other user. But the problem is the same, other user can make changes in the parent between the parent is load and the save changes.
So my question is, if I want to delete the parent and all its children with EF, which is the best way? The best way is try N times delete the parent? is this a good option when I wnat a good performance?
Thanks.
Daimroc.
You've answered your own question. Use cascading deletes. You can enable them using EF code first, see How do you ensure Cascade Delete is enabled on a table relationship in EF Code first?, or using the designer by marking the relationship as cascading.
In this particular case I think that the best way is to use a trigger to delete de childs.
I have data in following two tables
Parent Mater and Child Master
In parent master there are two fields prtID and name. In child master there are three fields ID, name and prtid, The prtid is foreign key from parent master.
I want to bind data with TreeView in a windows application in c#.net
such that each child will appear inside its parent node.
Can please guide me to right direction for this task?
Looks like duplicate entry. Please do some search before making a new question :
Bind C# Windows Form TreeView from DataBase
Presently In my application , on click of grid row , i fill the control of the page with the grid row entries. I have a UPDATE button to save the changes . But even if the don't modify any control values and click on Update button , updation will be done with same values. that triggers a entry to log table .
How can I make the form to call the update only when the values of the controls are different than what is being loaded to them when i click grid.
UPDATE :
Application is not using any pattern like MVVM . the structure what is being followed is very slimier to win forms except the UI . For db interaction ADO.net . It was built already when i joined to this project & even I am new to wpf.
Can anyone help me out ?
I think that you need to do some change tracking on the Entities displayed in the grid. Whenever an Update is executed you check the state of the entities and only update those entities where it's needed. Basically you need to intercept at the level of Property Setters to keep track of changes in the entity. think about:
Using a base class that has some functionality for keeping track of the state of an entity. Very simple: a Boolean hasChanged or more "complex" an Enum with Added, Modified and Deleted or something.
Derive other entities from this base class
Work with public properties and private backing fields to intercept property modifications
When property modifications are executed store the information in a Boolean or Enum in the base class
When the update is initiated by the user loop through the entities presented in the Grid and pick those that have changes and send them to the database.
Some helpfull theory about this might be:
Change tracking on businees objects
Using Properties C# programming guide
ADO.NET Self Tracking Entities
I don't know from your question how tightly the coupling is between the user interface and the database (ADO.NET DataTables or so...). The idea presented above requires some "decoupling" of things. But, is a good basis for further functionalities in your application and can give you some ease of work, clear framework, improved maintenance and might increase performance.
I believe the easiest way to do this is to add a handler to controls, that are used to get user input, that will set some boolean property, e.d. RowWasEdited, to true. Then check this property on UPDATE button click event, do neccessary actions, and set it to false.
But I'm sure that there is something more elegant than this.
I have a Windows Form with some textboxes and a Save button. When the form loads the textboxes are populated with data from an entity in my model. When the user clicks on the save button the values in each textbox are written back to the entity and then SaveChanges is called to commit the data to the database.
What I'd like to know is what is the best way to check if the form contains changes? If it doesn't contain changes then I needn't call SaveChanges and I can save writing the record back to the database. If it does contain changes and the user hasn't clicked on the Save button I want to get the user's confirmation that the changes don't need to be saved.
I thought maybe I could just update the entity's fields and then check its State property before calling SaveChanges but this fails as updating any field, even with an identical value, causes the entity to be marked as modified.
So, my question is, what is the best way to check that changes have actually been made to the form before calling SaveChanges?
Thanks,
Matt
You can check the entity state. Just save the data from the textboxes to the entity ans see if the EntityState is EntityState.Unchanged.
Details here: http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx
Actually updating the field even with the same value as the previous one counts as a modified entity and in most cases this is the correct business rule.
What you could do is keep a copy of the original object that was used to fill the form fields and compare it with the current one using an equality comparer. It's not pretty but it gets the job done in particular cases where you cannot count on the object state manager's opinion of modified.