This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi,
How do I update the database with changes made to the grid?
I connected the grid like this:
private void cbRicerca_Click(object sender, EventArgs e)
{
using (ModelUnitOfWork uow = Repository.Context.CreateUnitOfWork())
{
gridControl1.DataSource = uow.Find();
}
}
Thanx!
|
|
|
You don't say whether this is a Web or WinForms application; from the idiom I am assuming WinForms (let me know if it's Web). In this case, you have two options: 1. When you need to save the data, create a new unit of work and Attach the entities from the grid to it. Then call SaveChanges() on the new unit of work. 2. Keep the unit of work around for the lifetime of the form instead of disposing it immediately. (For example, make uow a member variable.) When you need to save the data, call SaveChanges() on the unit of work. Don't forget to dispose the unit of work in the form's Dispose override. The first strategy is a little bit more fiddly but keeps the units of work short-lived, maximising the efficiency of using database connections and avoiding problems if there is a network error. The second is more convenient but holds a connection open, so should not be used if the edit session is likely to take more than a few minutes. |
|
|
Is a WinForm application. I'm trying the first option, but I can not do attach the entity. This is the code: private void cbSalva_Click(object sender, EventArgs e) And this is the error: 'Prove.Soggetti' is a 'type' but is used like a 'variable'.
What is wrong?
|
|
|
You need to grab the entity instances back from your data source. Thus: IList<Soggetti> soggetti = (IList<Soggetti>)(gridControl1.DataSource); (Pardon the mangled Italian; I hope I haven't written anything rude *grin*.) Depending on the exact Find method you used, the cast in the first line might need to be a bit different. In any case, the point is that you need to aattach each of the entities that came back from the Find to the unit of work. (Actually, you only need to add the ones that have changed, but don't worry about that.) Depending on how your grid control handles deletion, you may also need to special-case deleted entities. |
|
|
It works fine! Thanx
|
|
|
Hi Ivan, what until recently was working, now no longer works. The database is not updated anymore and I can not understand why. I don't have a specific error, it seems that everything is fine, but when I clean the grid (gridControl1.DataSource = null) and run the research (cbRicerca_Click), the data have not changed. All I ask you is some advice on the method of reading \ delete \ storage that I use, to see if the error might be there. Read : private void cbRicerca_Click(object sender, EventArgs e) Delete : private void cbElimina_Click(object sender, EventArgs e) Store : using (ModelUnitOfWork uow = Repository.Context.CreateUnitOfWork()) uow.Dispose(); Thanx. |
|
|
Hmm, that looks okay, unless there's a TransactionScope lurking around. If you attach a logger (Repository.Context.Logger = new TraceLogger()), what SQL do you see being sent for the SaveChanges operation? If you don't see anything, it means that the entities in the grid are not being changed (or not being marked as changed). If you see INSERT and UPDATE statements, it means that the changes are being sent to the database, but are not being persisted (or for some reason aren't being reloaded: a quick check in the database will establish which). Could you create a small project that reproduces the problem, including SQL CREATE TABLE scripts and instructions on what to do to cause the problem to happen, and post it here? You can attach a zip file via the Options tab. |
|