To use ROWVERSION in LINQ or not?

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?
brettrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.