简体   繁体   English

为什么Linq-to-SQL生成错误的代码,但是编译时没有错误

[英]Why is wrong code generated by Linq-to-SQL, but there's no error when compiling

I use Linq-to-SQL which will generate classes for database tables dragged to its designer. 我使用Linq-to-SQL,它将为拖动到其设计器的数据库表生成类。 When I drag Product table to designer, Visual Studio will generate like this: 当我将“ Product表拖到设计器时,Visual Studio将生成如下所示:

[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();
            }
        }
    }
     //...
}

OnProductIDChanging and OnProductIDChanged are not abstract methods and they are empty but why they don't give compiling-error. OnProductIDChangingOnProductIDChanged不是抽象方法,它们是空的,但是为什么它们没有给出编译错误。 Thanks. 谢谢。

Because they're partial methods . 因为它们是局部方法 From MSDN: 从MSDN:

One part of the class contains the signature of the method. 类的一部分包含方法的签名。 An optional implementation may be defined in the same part or another part. 可以在同一部分或另一部分中定义一个可选实现。 If the implementation is not supplied, then the method and all calls to the method are removed at compile time. 如果未提供实现,则在编译时删除该方法和对该方法的所有调用。

Because they are partial methods. 因为它们是局部方法。

This article has a very good explanation of partial methods. 本文对部分方法有很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM