– Mohammadreza Jan 13 '09 at 21:01 Hard way: Write your own DataContext and DataTable(T) classes from scratch. – David B Jan 13 '09 at 21:05.
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.
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 Jan 13 '09 at 21:11.
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.
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 Jan 13 '09 at 21:04.
See here: stackoverflow.com/questions/317114/deter... 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.
Pass the data context as a ref parameter to a custom method on your partial Product object: public partial class Product { public string GetSomethingElse(ref DataContext dbase) { return dbase. OtherTableWhatever.Count.ToString(); // whatever } } Inside your aspx. Cs: var context = new DataContext(); var product = context.Products.
SingleOrDefault(x => x. Id == 1); var s = product. GetSomethingElse(ref context).
YES, do the following: public partial class Product { public YourDbDataContext Context { get{return new YourDbDataContext();} } }.
That's returning another data context, not the one that owns this Product instance. – GeekyMonkey Jun 22 '10 at 21:48.
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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.