views:

885

answers:

5

Is there anyway simple ways to add a property to Linq to Sql generated entity to reference its DataContext? For example:

var myContext = new DataContext(); 
var product = context.Products.Where(p => p.Id == productId).SingleOrDefault();

and product entity has a property called "Context" (product.Context) that has a reference to the myContext, datacontext.

I know how to customize generated entities. My question is how to customize (i think DataContext) to set 'Context' property of each instance that it creates to itself.

I'm not sure I'm doing the right thing or not. I want to write a business encapsulation model that has best performance with less code. As i googled around I have find out that the DataContext object is very lightweight and there for I thought it would be a good way to add an instance of DataContext to each object. This will reduce the need to attach, detached objects again to a new instance of datacontext every time I want to update or delete them.

If you have any other solution I will really appreciate it.

Thanks

+2  A: 

Is there a simple way to add a property to Linq to Sql generated entity to reference its DataContext?

There is no simple way to achieve this.

David B
Ok, what is the hard way?
Mohammadreza
Hard way: Write your own DataContext and DataTable(T) classes from scratch.
David B
+1  A: 

In doing this, you defeat part of the purpose of LINQ-to-SQL. One of the purposes is to allow to you work with the objects that you have, and not have to change your design based on database considerations.

In attaching the DataContext to the database, you are coupling your representation in code with the means to persist it, which is generally a bad design idea.

If you feel you must do this though, you can always derive from the class and then implement an interface on that class which exposes the DataContext.

I recommend implementing the interface, since you can't indicate a base class to the LINQ-to-SQL designer, and you want to be able for all of your entities to have a shared implementation.

casperOne
I agree, but my application has a lot of request for update and it doesn't required timestap, so I can not attach it to a new DataContext and have to SELECT my object with its key again, merge it and then update it. I think there is a lot of performance lost in it. Am I wrong?
Mohammadreza
A: 

Actually, I agree to casperOne. If you really have to need this, I remembered that the classes that linq-to-sql generates are partial. So you can write a partial class to any class you want and add extended functionalities to it.

yapiskan
sure, but the problem is when using a linq query it creates an instance of the class that extended property of it (in my case DataContext) is not initialized.
Mohammadreza
A: 

See here: http://stackoverflow.com/questions/317114/determine-the-source-datacontext-for-a-linq-to-sql-query

I asked more or less the same question. You can get the context from the IQueryable that a linq to sql query returns, but not from the entity itself as far as I know.

Rune Grimstad
A: 

YES,

do the following:

public partial class Product
{
    public YourDbDataContext Context
    {
        get{return new YourDbDataContext();}
    }

}
Khaled Musaied
That's returning another data context, not the one that owns this Product instance.
GeekyMonkey