views:

461

answers:

1

The Setup:

I have a large form with many fields that are collected to update a Product object. So in the ASPX page the user changes the fields that need updating and they hit submit. In the code behind I do something like this;

Dim p as New MyCompany.Product()
p = p.GetProductById(ProductID)

I extend the Product partial class of Linq to SQL to add this method (GetProductById) to the object

p.Name = txtName.Text
p.SKU = txtSKU.Text
p.Price = txtPrice.Text
...    
p.Update()

This is an Update method in the extended Product partial class. I update the database, send emails and update history tables so i want this method to do all those things.

There are 50 more fields for the project so obviously it would be ridiculous to have a method that collects all 50 fields (and I don't want to go that route anyway bc it's harder to debug IMO)

The Problem:

If I get the Product via Linq to SQL using a DataContext then I can never update it again because it errors about not being able to attach and entity that's already attached to another DataContext.

The Question:

SO if I get an object through a method in my BLL, update it in the ASPX page and then try to send the updates through the BLL again to update the database, how should I go about doing this?

A: 

Regardless of LINQ-to-SQL or not, here's what I do. Upon submission, I search for the item (it should be quick if it is a single item using the PK), my DAL returns a data object, and I use reflection to map each element in the page with corresponding properties in the data object. My DAL only updates items that changed.

I think what you have to do is the same, gathering all values and submitting them. If LinqToSql is not smart enough to determine what changed then it may not be the best alternative.

Otávio Décio
Well, me not using Linq to SQL at this point is not an option. I can't go back and redo my entire application just because of this one issue. Thanks for your response though. I'll keep looking for a Linq-to-Sql-specific answer.
EdenMachine