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
DELETE
was 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; }
}
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.