技术开发 频道

LINQ to SQL:创建你的第一个程序


    第三步,在App_Code文件夹下添加LINQ to SQL Classes类,并在服务器浏览器中拖拽第一步我们所建的Customers表到设计界面:



    经过了上面的操作之后,在新建的LINQ to SQL类中做了什么?打开刚才所建的LINQ to SQL类设计文件(.designer.cs),可以看到,首先定义了一个DemoDataClassesDataContext类,并为它配置了名为Database的特性,DataContext(数据上下文)类是实体类和数据库之间的一个桥梁,Database特性配置了该DataContext与哪个数据库所对应:

Code2:

[System.Data.Linq.Mapping.DatabaseAttribute(Name="Demo")] 
public partial class DemoDataClassesDataContext : System.Data.Linq.DataContext
{
//……
}
    同时,还定义了一个名为Customer的实体类,该类是对数据库表的描述,通过Table特性来指定它与哪张表映射,通过Column特性来指定属性与数据库表中的字段之间的对应关系,关于DataContext(数据上下文)和实体的映射,后续的文章中我还会专门去讲述。
Code3:
[Table(Name="dbo.Customers")] 
public partial class Customer : INotifyPropertyChanging, INotifyPropertyChanged
{
private int _Id;

private string _Name;

#endregion

[Column(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int Id
{
get
{
return this._Id;
}
set
{
if ((this._Id != value))
{
this.OnIdChanging(value);
this.SendPropertyChanging();
this._Id = value;
this.SendPropertyChanged("Id");
this.OnIdChanged();
}
}
}
}

0
相关文章