2011-10-27 18 views
1

我使用Linq-to-SQL,它將爲拖動到其設計器的數據庫表生成類。當我拖累Product表設計器,Visual Studio會產生這樣的:爲什麼Linq-to-SQL生成的代碼錯誤,但是在編譯時沒有錯誤

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Products")] 
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged 
{ 
    // ... 
    partial void OnProductIDChanging(int value); 
    partial void OnProductIDChanged(); 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ProductID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] 
    public int ProductID 
    { 
     get 
     { 
      return this._ProductID; 
     } 
     set 
     { 
      if ((this._ProductID != value)) 
      { 
       this.OnProductIDChanging(value); 
       this.SendPropertyChanging(); 
       this._ProductID = value; 
       this.SendPropertyChanged("ProductID"); 
       this.OnProductIDChanged(); 
      } 
     } 
    } 
    //... 
} 

OnProductIDChangingOnProductIDChanged不是抽象的方法,他們是空的,但他們爲什麼不給編譯錯誤。謝謝。

+0

你是什麼意思?爲什麼編譯器會觸發空方法的錯誤? –

回答

3

因爲他們是partial methods。從MSDN:

該類的一部分包含該方法的簽名。可選的實施方式可以在相同的部分或其他 部分中定義。如果未提供該實現,則在編譯時刪除方法和對該方法的所有調用。

+0

+1和那些部分方法**完全有效** C#代碼! –

相關問題