J
Jakob Lithner
I have a DBML object that maps database to objects.
In the partial Text class I added a couple of calculated properties.
Several of them are dependant on how many child object are related to the
current object. To avoid duplicate database calls I got the idea to set local
variable _childrenCount in the Text OnLoaded event.
partial void OnLoaded()
{
// ChildrenCount is used in many calculated properties and is therefore
set already on Load.
using (MyDataContext dc = MyDataContext.CreateInstance())
{
_childrenCount = dc.Texts.Where(t => (t.TextIDParent == this.TextID)
&& (t.TextID != this.TextID)).Count();
}
}
I found no way to refer to and reuse the current datacontext object to do
the call, therefore I created an instance method on the datacontext object to
provide a new object. But it really looks like a workaround.
The above solution actually worked fine until I added TransactionScope to my
code. When a retrieved Text object is involved in a transaction it will fail
with the following message: "MSDTC on server XXX is unavailable". When the
above event handler is removed everything works fine.
I guess the transaction will detect a new datacontext object is called and
expect it to take part in the transaction. Actually it should not bother
beacuse it is only a passive read.
1) Is it possible to reuse the outer datacontext call for the inner
calculation?
2) Can I tell the inner datacontext not to take part in the outer transaction?
3) Is there a better way to set the value of the inner private variable
_childrenCount?
4) Should I configure DTC and/or exchange TransationScope with Transaction?
In the partial Text class I added a couple of calculated properties.
Several of them are dependant on how many child object are related to the
current object. To avoid duplicate database calls I got the idea to set local
variable _childrenCount in the Text OnLoaded event.
partial void OnLoaded()
{
// ChildrenCount is used in many calculated properties and is therefore
set already on Load.
using (MyDataContext dc = MyDataContext.CreateInstance())
{
_childrenCount = dc.Texts.Where(t => (t.TextIDParent == this.TextID)
&& (t.TextID != this.TextID)).Count();
}
}
I found no way to refer to and reuse the current datacontext object to do
the call, therefore I created an instance method on the datacontext object to
provide a new object. But it really looks like a workaround.
The above solution actually worked fine until I added TransactionScope to my
code. When a retrieved Text object is involved in a transaction it will fail
with the following message: "MSDTC on server XXX is unavailable". When the
above event handler is removed everything works fine.
I guess the transaction will detect a new datacontext object is called and
expect it to take part in the transaction. Actually it should not bother
beacuse it is only a passive read.
1) Is it possible to reuse the outer datacontext call for the inner
calculation?
2) Can I tell the inner datacontext not to take part in the outer transaction?
3) Is there a better way to set the value of the inner private variable
_childrenCount?
4) Should I configure DTC and/or exchange TransationScope with Transaction?