欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

linq.designer.cs学习笔记

发布时间:2025/7/25 编程问答 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linq.designer.cs学习笔记 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

#pragma warning disable 1591

去除1591的警告信息

 

[System.Data.Linq.Mapping.DatabaseAttribute(Name="Development")]

设置linq连接的数据库为"Development"

 

public partial class LinQDBDataContext : System.Data.Linq.DataContext

partial这是声明分部类的一个关键字,为C#2.0中新增的, 一个类可以分成很多部分,写在不同的地方

继承了Linq.DataContext类,主要实现对数据库对象访问的方法

 

private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

定义私有静态变量mappingSource,绑定为前面定义的DatabaseAttribute(Name=" ")

 

#region Extensibility Method Definitions

#endregion

#region C# 预处理器指令。

#region 是一个分块预处理命令,它主要是用于编辑器代码的分块,在编译时会被自动删除。

#region 使您可以在使用 Visual Studio 代码编辑器的大纲显示功能时指定可展开或折叠的代码块。

 

public LinQDBDataContext():base(global::System.Configuration.ConfigurationManager.ConnectionStrings["DevelopmentConnectionString"].ConnectionString, mappingSource)

base的作用://子类的构造函数继承的为父类第几个构造函数(这里继承自含有2个参数的DataContext的构造函数)

这个构造函数通过读取web.config中的connectionStrings来生成LinQDBDataContext对象

linq会自动在web.config中生成对数据库的配置项

<connectionStrings>

  <add name="DevelopmentConnectionString" connectionString="Data Source=CC-0264C27A02CC\SQLEXPRESS;Initial Catalog=Development;Integrated Security=True"

   providerName="System.Data.SqlClient" />

 </connectionStrings>

 

public LinQDBDataContext(string connection) : base(connection, mappingSource)

这个构造函数通过自定义connection来生成LinQDBDataContext对象

 

public LinQDBDataContext(System.Data.IDbConnection connection) : base(connection,mappingSource)

这个构造函数根据IDbConnection接口中定义的connection来生成LinQDBDataContext对象

 

public LinQDBDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource)

这个构造函数可以自定义connectionmappingSource

 

public LinQDBDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource)

这个构造函数根据IDbConnection接口中定义的connection和自定义的mappingSource来生成LinQDBDataContext对象

 

public System.Data.Linq.Table<ModuleTable> ModuleTable

     {

         get

         {

              return this.GetTable<ModuleTable>();

         }

     }

根据用户在数据库中创建的表自动生成,返回一个表对象的泛型集合

 

[Table(Name="dbo.ModuleTable")]

自动生成对每个表的对象

 

public partial class ModuleTable : INotifyPropertyChanging, INotifyPropertyChanged

INotifyPropertyChanging 是在 .NET Framework 3.5 版中引進的

INotifyPropertyChanging 介面是用來告知用戶端 (通常是繫結用戶端),屬性值正在變更。

 

private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

定义属性值变化对象

 

[Column(Storage="_ModuleName", DbType="VarChar(50) NOT NULL", CanBeNull=false)]

     public string ModuleName

     {

         get

         {

              return this._ModuleName;

         }

         set

         {

              if ((this._ModuleName != value))

              {

                   this.OnModuleNameChanging(value);

                   this.SendPropertyChanging();

                   this._ModuleName = value;

                   this.SendPropertyChanged("ModuleName");

                   this.OnModuleNameChanged();

              }

         }

     }

绑定每列的属性,setget方法

 

#pragma warning restore 1591

恢复1591的警告信息

转载于:https://www.cnblogs.com/sleeplessC/articles/1612911.html

《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读

总结

以上是生活随笔为你收集整理的linq.designer.cs学习笔记的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。