Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I am using Visual Studio 2010, SQL Server 2008 R2 developing on Windows 7 Ultimate. I am using .NET Framework 4 and C# to develop a WinForms application that interfaces to an existing SQL Server database using Entity Framework 5 Code First technique of reverse engineering the DbContext and POCOs from the database and then developing.
 
My question is how to handle concurrency. So the WinForms GUI implements two-way databinding via this article here[^]. There is a requirement to make the app a WinForms since the customer has security constraints that only permit staff to use their database via a Smart Client and when I got to this client they had already started the WinForms project.
 
What would really answer this question great are any good links to articles. I have tried to Google search this; however, I am not quite sure I even understand what exactly (terminology-wise etc) I should even be querying Google on.
 
Basically my question is: Being a newbie to EF Code First, what is the proper handling in order to safely do the following:
 
1. User adds a new row in edit mode in my grid view, makes some changes and finishes editing.
2. Then user inserts two new records into the table using SQL Server Management Studio (SSMS) by just running a plain old insert query (say this mimics data flowing into the database table from an outside sender whilst we are also viewing the grid view in the WinForms app)
3. Then user does a DbContext.Save Changes() method call on the WinForms app by clicking the Save button.
 
Two questions:
 
1) How can I make my Grid View know that an INSERT, UPDATE, or DELETEwas done on the underlying database apart from the application and keep the gridview current?
 
2) How can i make sure that if records are added to the grid view, then changes happen directly on the database itself and then I go back to the Winforms app and do a save, that my results will be "combined" properly? Right now, some really strange bastard thing happens and I get unpredictable results.
 

I am going to use the trivial Person example table and POCOs to protect my client's data references. Okay, so my POCO is:
 
public class Person 
{
    public int Id { get; set; }
    public String LastName { get; set; }
    public String FirstName { get; set;}
    public Date DateOfBirth { get; set; }
}
 
And my DbContext is defined as follows (greatly simplified because we all know the other stuff that really goes in a DbContext but I am not wanting to bury my question in too excruciating of detail):
 
public class PersonContext : DbContext 
{
    public DbSet<Person> Persons { get; set; }
 
    // Constructors etc blah blah
}
 
And of course I think the design of the underlying SQL Server database table must be obvious as you can infer it from the POCO fields. Max length is arbitrary and that has no bearing on my question.
 
Of course, using a production situation and foreign keys etc is a totally and completely different ball of wax.
 
Thanks.
Posted 9-Apr-13 7:03am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 204
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 183
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100