LINQ to SQL - dyanamic updates

  • Thread starter Cowboy \(Gregory A. Beamer\)
  • Start date
C

Cowboy \(Gregory A. Beamer\)

Just wanted to ping this off of someone and what better place to do it than
here.

I have the following:
public void Delete(T entity)
{
Type type = entity.GetType();
//TODO: Can make this dynamic for flexibility
string property = "IsDeleted";

PropertyInfo propertyInfo = type.GetProperty(property);

if (propertyInfo == null)
{
Destroy(entity);
}
else
{
using (System.Data.Linq.DataContext context =
_dataContextFactory.Context)
{
Table<T> table = context.GetTable<T>();
//TODO: Figure out why .Attach() is not working and stop
grabbing object
entity = table.First(s => s == entity);

MethodInfo methodInfo = propertyInfo.GetSetMethod();
object[] o = { true };
methodInfo.Invoke(entity, o);

context.SubmitChanges();
}
}
}

It works, but there must be an easier way to save than to find the object
prior to making changes. The destroy method works fine,. but having to
search for the object prior to Submitting changes seems rather cheese to me.
Thoughts?

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 
C

Cowboy \(Gregory A. Beamer\)

I have updated on my blog and it seems to be working. Dynamically finding
the primary key seems to be the best option. Still have to find the object
using the data context, but it flows nicely.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top