?
Solved

To use ROWVERSION in LINQ or not?

Posted on 2008-11-17
1
Medium Priority
?
935 Views
Last Modified: 2013-11-11
In .NET 3.5, when I want to update and object and save its results, I query it from the database, assign new values and SubmitChanges().  This means I can overwrite someone else's changes.   As an example, each "T" means time.  DataContexts are created in a using statement:

T1: PersonA queries and loads ID=49 of a Person object and assigns new address without SubmitChanges().
T2: PersonB queries ID=49 of a Person object, updates and SubmitChanges().
T3: PersonA queries ID=49 of a Person, updates with values from T1 and SubmitChanges().

In the example, PersonA wrote over PersonB's changes.  Although not entirely if only one or two properties were being updated.  Everything is fine if PersonB only changed PhoneNumber.  If I go with a  ROWVERSION and keep a DataContext around inside the Person object, what will happen in this situation?

Not using ROWVERSION is the simpler solution but I will always potentially overwrite someone's changes in the above scenario.   What is the best way to handle this case?
0
Comment
Question by:brettr
1 Comment
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 22977130
Hi brettr;
If you have your data context and table class set up like this then yes.
 
public class Northwind : DataContext
{
    private static readonly string connectionString = @"Data Source=.\sqlexpress;" + 
        "Initial Catalog=Northwind; Integrated Security=true";
    public Northwind()
        : base(connectionString)
    {
        Log = Console.Out;
    }
 
    public Table<Shipper> Shippers
    {
        get { return this.GetTable<Shipper>(); } 
    }
}
 
[Table(Name = "Shippers")]
public class Shipper
{
    [Column(IsPrimaryKey = true)]
    public int ShipperID { get; set; }
 
    [Column()]
    public string CompanyName { get; set; }
 
    [Column()]
    public string Phone { get; set; }
}
 
Changing the table class to this will throw an exception in the situation you are talking about
 
[Table(Name = "Shippers")]
public class Shipper
{
    [Column(IsPrimaryKey = true)]
    public int ShipperID { get; set; }
 
    [Column(UpdateCheck=UpdateCheck.WhenChanged)]  // Will check this field each time it updated
    public string CompanyName { get; set; }
 
    [Column(UpdateCheck=UpdateCheck.Never)]        // This column is never check and will update if no other fileds are in conflect
    public string Phone { get; set; }
}
 
You will need to do a try/catch to catch the exception
 
try                                                                    
{                                                                      
    // submit changes with the conflect mode set to FailOnFirstConflict
    northwind.SubmitChanges(ConflictMode.FailOnFirstConflict);         
}                                                                      
catch (ChangeConflictException ex)                                     
{                                                                      
    // Handle your conflect here                                       
    Console.WriteLine("\n" + ex.Message);                              
}    

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question