Wednesday
Apr092008
LINQ to SQL - code generation bug
Wednesday, April 9, 2008 at 5:23PM
The code generation performed by MSLinqToSQLGenerator or SQLMetal generates weird property code. For example, in AdvantureWorks, the table Product has a column ProductLine. Using the tools that come with LINQ to SQL, this column translates to a property:
All classes created by LINQ to SQL add the following protected methods:
Here is the field that is passed to all invocations of the event:
As you can see from this constructor, the property that is changing is an empty string. Given the fact that this is a private field and should not be modified by extension methods, it’s odd that this field is not static readonly.
My guess is that the code is generated incorrectly to account for a data-binding or allocation problem. I’ve come to this conclusion by the emptyChangingEventArgs field – it reduces the object instance creation in half for each property change when there are event consumers for the changing event. The big disadvantage for event consumers is that they doesn’t know which property is changing on an object instance.
One alternative is to use PLINQO, which creates the properties correctly.
UPDATE: I have found that this has already been reported. Unfortunately, Microsoft has closed this bug and said it was by design, even though the generated code does not follow Microsoft's documentation for the PropertyChangingEventArgs class.
[Column(Storage="_ProductLine", DbType="NChar(2)")]The odd code involves the SendPropertyChanging() method call. This method call should pass the name of the property, just like the SendPropertyChanged() method call, according to the documentation. Another interesting detail: The OnProductLineChanging and OnProductLineChanged partial method calls are out of order:
public string ProductLine
{
get
{
return this._ProductLine;
}
set
{
if ((this._ProductLine != value)) {
this.OnProductLineChanging(value);
this.SendPropertyChanging();
this._ProductLine = value;
this.SendPropertyChanged("ProductLine");
this.OnProductLineChanged();
}
}
}
- Call OnProductLineChanging() partial method
- Raise PropertyChanging event, but don’t tell which property is changing – send an empty string instead
- Set the property’s field’s value to the specified new value
- Raise PropertyChanged event and specify which property is changing
- Call OnProductLineChanged() partial method
All classes created by LINQ to SQL add the following protected methods:
Again, what’s odd about this code is how the SendPropertyChanging() method does not have a property name parameter and sends a emptyChangingEventArgs field reference to the PropertyChanging event rather than creating a new instance of the EventArgs like the SendPropertyChanged() method call does. By creating a new instance of the EventArgs in SendPropertyChanged, it’s able to pass the property name in the constructor (like the documentation says it should).
protected virtual void SendPropertyChanging() {
if ((this.PropertyChanging != null)) {
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName) {
if ((this.PropertyChanged != null)) {
this.PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
Here is the field that is passed to all invocations of the event:
private static PropertyChangingEventArgs emptyChangingEventArgs =
new PropertyChangingEventArgs(String.Empty);
As you can see from this constructor, the property that is changing is an empty string. Given the fact that this is a private field and should not be modified by extension methods, it’s odd that this field is not static readonly.
My guess is that the code is generated incorrectly to account for a data-binding or allocation problem. I’ve come to this conclusion by the emptyChangingEventArgs field – it reduces the object instance creation in half for each property change when there are event consumers for the changing event. The big disadvantage for event consumers is that they doesn’t know which property is changing on an object instance.
One alternative is to use PLINQO, which creates the properties correctly.
UPDATE: I have found that this has already been reported. Unfortunately, Microsoft has closed this bug and said it was by design, even though the generated code does not follow Microsoft's documentation for the PropertyChangingEventArgs class.

Reader Comments (1)
As a famous Paris Fashion brand, Herve Leger Strapless is taking great effort to show women`s
perfect figures and gentle charactors. 2010 new style Herve Leger help women to show this line of beauty
perfectly. This is also the tenet of Herve Leger Strapless .Originating in France,2010 new style
Herve Leger also have the charactor as Paris Women does, natural but not fake, romantic, elegant
and Vogue .In the 1980s, Moncler Polo Shirt become unprecedentedly popular all over the
world. They are poplular because all the
Moncler Jackets
Vest are made of high- quality down.So many young people are fascinated with Moncler Polo
Shirt , and we guess you must be one of them. Start to be the trend-spotter from
owning an Moncler
Accessories.The red outsole is the distinctive features of
Christian Louboutin Boots , also is the female of gentle, lovely, beautiful
and sexy logo.Soon, the red high-heeled Christian Louboutin Flats spread all over the
world after Cinderella's fairy tales, especially the big stars and royal aristocrats let
Christian Louboutin Flats appear in the front of the world. Christian
Louboutin Boots favor gorgeous colors with various exotic. . No matter what kinds
of HERMES PURSE you
like, you can have a look at HERMES BELT .Please go and check out, you will be attracted by those
beautiful and elegant belt. And among the various Hermes Kelly , there must be one suitable for you.Hermes
Lindy&Hermes Evelyne one of the best handbag brands in the world.