技术开发 频道

C#3.0-OR工具介绍


【IT168技术文档】

  先讲sqlmetal. 自上次的版本后,sqlmetal,又增加了一些新的功能。比如,支持SQLCE版本的Sql Server, 支持直接输入connection string, 等等。

  因为sqlmetal已经讲过,不再多讲,大家知道它是个命令行的工具,用来做映射数据库信息的,会使用就可以。再接着讲O/R Designer. 新建任一工程。右击工程,选择add->new item. 如图所示:

  弹出item对话框,category中选择data,然后选择Linq to Sql Class, 如图所示:

  选择添加。这样,你的工程就多了一个O/R Desiger项目。在菜单View->Sever Explore 打开Sever Explore,添加一个Data Connection, 如图所示:

  后,填写数据库服务器,用户名,密码和数据库名。如果,你没有,你可以使用Sql Express 版本。添加之后的效果,如图所示:

  打开刚才添加的DataClasses1.dbml 文件,将Sever Explore中的Customer 表或view拖入Designer中来,如图所示:

  如果,两个表之间有关系,OR Designer可以显示它们的关系,将Order表和 OderDetail表拖入 如图所示:

  打开DataClasses1. Designer.cs 文件,你就可以看到OR Designer给你产生的映射代码了。
using System.Data.Linq; using System.Data.Linq.Mapping; using System.Data; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; using System.ComponentModel; using System; [System.Data.Linq.Mapping.DatabaseAttribute(Name="northwind_may06ctp")] public partial class DataClasses1DataContext : System.Data.Linq.DataContext { private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); Extensibility Method Definitions#region Extensibility Method Definitions partial void OnCreated(); partial void InsertOrder(Order instance); partial void UpdateOrder(Order instance); partial void DeleteOrder(Order instance); partial void InsertOrder_Detail(Order_Detail instance); partial void UpdateOrder_Detail(Order_Detail instance); partial void DeleteOrder_Detail(Order_Detail instance); #endregion static DataClasses1DataContext() { } public DataClasses1DataContext(string connection) : base(connection, mappingSource) { OnCreated(); } public DataClasses1DataContext(System.Data.IDbConnection connection) : base(connection, mappingSource) { OnCreated(); } public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public DataClasses1DataContext() : base(global::SqlMetalUtil.Properties.Settings.Default.northwind_may06ctpConnectionString, mappingSource) { OnCreated(); } public System.Data.Linq.Table<Order> Orders { get { return this.GetTable<Order>(); } } public System.Data.Linq.Table<Order_Detail> Order_Details { get { return this.GetTable<Order_Detail>(); } } [Function(Name="dbo.CategoriesInsert")] public int CategoriesInsert([Parameter(Name="@CategoryID", DbType="int")] System.Nullable<int> CategoryID, [Parameter(Name="@CategoryName", DbType="nvarchar")] string CategoryName, [Parameter(Name="@Description", DbType="ntext")] string Description, [Parameter(Name="@Picture", DbType="image")] byte[] Picture) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), CategoryID, CategoryName, Description, Picture); return ((int)(result.ReturnValue)); } } [Table(Name="dbo.Orders")] public partial class Order : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private int _OrderID; private string _CustomerID; private System.Nullable<int> _EmployeeID; private System.Nullable<System.DateTime> _OrderDate; private System.Nullable<System.DateTime> _RequiredDate; private System.Nullable<System.DateTime> _ShippedDate; private System.Nullable<int> _ShipVia; private System.Nullable<decimal> _Freight; private string _ShipName; private string _ShipAddress; private string _ShipCity; private string _ShipRegion; private string _ShipPostalCode; private string _ShipCountry; private EntitySet<Order_Detail> _Order_Details; Extensibility Method Definitions#region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(); partial void OnCreated(); partial void OnOrderIDChanging(int value); partial void OnOrderIDChanged(); partial void OnCustomerIDChanging(string value); partial void OnCustomerIDChanged(); partial void OnEmployeeIDChanging(System.Nullable<int> value); partial void OnEmployeeIDChanged(); partial void OnOrderDateChanging(System.Nullable<System.DateTime> value); partial void OnOrderDateChanged(); partial void OnRequiredDateChanging(System.Nullable<System.DateTime> value); partial void OnRequiredDateChanged(); partial void OnShippedDateChanging(System.Nullable<System.DateTime> value); partial void OnShippedDateChanged(); partial void OnShipViaChanging(System.Nullable<int> value); partial void OnShipViaChanged(); partial void OnFreightChanging(System.Nullable<decimal> value); partial void OnFreightChanged(); partial void OnShipNameChanging(string value); partial void OnShipNameChanged(); partial void OnShipAddressChanging(string value); partial void OnShipAddressChanged(); partial void OnShipCityChanging(string value); partial void OnShipCityChanged(); partial void OnShipRegionChanging(string value); partial void OnShipRegionChanged(); partial void OnShipPostalCodeChanging(string value); partial void OnShipPostalCodeChanged(); partial void OnShipCountryChanging(string value); partial void OnShipCountryChanged(); #endregion public Order() { OnCreated(); this._Order_Details = new EntitySet<Order_Detail>(new Action<Order_Detail>(this.attach_Order_Details), new Action<Order_Detail>(this.detach_Order_Details)); } [Column(Storage="_OrderID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)] public int OrderID { get { return this._OrderID; } set { if ((this._OrderID != value)) { this.OnOrderIDChanging(value); this.SendPropertyChanging(); this._OrderID = value; this.SendPropertyChanged("OrderID"); this.OnOrderIDChanged(); } } } [Column(Storage="_CustomerID", DbType="NChar(5)")] public string CustomerID { get { return this._CustomerID; } set { if ((this._CustomerID != value)) { this.OnCustomerIDChanging(value); this.SendPropertyChanging(); this._CustomerID = value; this.SendPropertyChanged("CustomerID"); this.OnCustomerIDChanged(); } } } [Column(Storage="_EmployeeID", DbType="Int")] public System.Nullable<int> EmployeeID { get { return this._EmployeeID; } set { if ((this._EmployeeID != value)) { this.OnEmployeeIDChanging(value); this.SendPropertyChanging(); this._EmployeeID = value; this.SendPropertyChanged("EmployeeID"); this.OnEmployeeIDChanged(); } } } [Column(Storage="_OrderDate", DbType="DateTime")] public System.Nullable<System.DateTime> OrderDate { get { return this._OrderDate; } set { if ((this._OrderDate != value)) { this.OnOrderDateChanging(value); this.SendPropertyChanging(); this._OrderDate = value; this.SendPropertyChanged("OrderDate"); this.OnOrderDateChanged(); } } } [Column(Storage="_RequiredDate", DbType="DateTime")] public System.Nullable<System.DateTime> RequiredDate { get { return this._RequiredDate; } set { if ((this._RequiredDate != value)) { this.OnRequiredDateChanging(value); this.SendPropertyChanging(); this._RequiredDate = value; this.SendPropertyChanged("RequiredDate"); this.OnRequiredDateChanged(); } } } [Column(Storage="_ShippedDate", DbType="DateTime")] public System.Nullable<System.DateTime> ShippedDate { get { return this._ShippedDate; } set { if ((this._ShippedDate != value)) { this.OnShippedDateChanging(value); this.SendPropertyChanging(); this._ShippedDate = value; this.SendPropertyChanged("ShippedDate"); this.OnShippedDateChanged(); } } } [Column(Storage="_ShipVia", DbType="Int")] public System.Nullable<int> ShipVia { get { return this._ShipVia; } set { if ((this._ShipVia != value)) { this.OnShipViaChanging(value); this.SendPropertyChanging(); this._ShipVia = value; this.SendPropertyChanged("ShipVia"); this.OnShipViaChanged(); } } } [Column(Storage="_Freight", DbType="Money")] public System.Nullable<decimal> Freight { get { return this._Freight; } set { if ((this._Freight != value)) { this.OnFreightChanging(value); this.SendPropertyChanging(); this._Freight = value; this.SendPropertyChanged("Freight"); this.OnFreightChanged(); } } }
0
相关文章