C# 3.0入门系列(七)--之OR工具介绍(下)

打开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.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();
        }
      }
    }

    [Column(Storage="_ShipName", DbType="NVarChar(40)")]
    public string ShipName
    …{
      get
      …{
        return this._ShipName;
      }
      set
      …{
        if ((this._ShipName != value))
        …{
          this.OnShipNameChanging(value);
          this.SendPropertyChanging();
          this._ShipName = value;
          this.SendPropertyChanged("ShipName");
          this.OnShipNameChanged();
        }
      }
    }

    [Column(Storage="_ShipAddress", DbType="NVarChar(60)")]
    public string ShipAddress
    …{
      get
      …{
        return this._ShipAddress;
      }
      set
      …{
        if ((this._ShipAddress != value))
        …{
          this.OnShipAddressChanging(value);
          this.SendPropertyChanging();
          this._ShipAddress = value;
          this.SendPropertyChanged("ShipAddress");
          this.OnShipAddressChanged();
        }
      }
    }

    [Column(Storage="_ShipCity", DbType="NVarChar(15)")]
    public string ShipCity
    …{
      get
      …{
        return this._ShipCity;
      }
      set
      …{
        if ((this._ShipCity != value))
        …{
          this.OnShipCityChanging(value);
          this.SendPropertyChanging();
          this._ShipCity = value;
          this.SendPropertyChanged("ShipCity");
          this.OnShipCityChanged();
        }
      }
    }

    [Column(Storage="_ShipRegion", DbType="NVarChar(15)")]
    public string ShipRegion
    …{
      get
      …{
        return this._ShipRegion;
      }
      set
      …{
        if ((this._ShipRegion != value))
        …{
          this.OnShipRegionChanging(value);
          this.SendPropertyChanging();
          this._ShipRegion = value;
          this.SendPropertyChanged("ShipRegion");
          this.OnShipRegionChanged();
        }
      }
    }

    [Column(Storage="_ShipPostalCode", DbType="NVarChar(10)")]
    public string ShipPostalCode
    …{
      get
      …{
        return this._ShipPostalCode;
      }
      set
      …{
        if ((this._ShipPostalCode != value))
        …{
          this.OnShipPostalCodeChanging(value);
          this.SendPropertyChanging();
          this._ShipPostalCode = value;
          this.SendPropertyChanged("ShipPostalCode");
          this.OnShipPostalCodeChanged();
        }
      }
    }

    [Column(Storage="_ShipCountry", DbType="NVarChar(15)")]
    public string ShipCountry
    …{
      get
      …{
        return this._ShipCountry;
      }
      set
      …{
        if ((this._ShipCountry != value))
        …{
          this.OnShipCountryChanging(value);
          this.SendPropertyChanging();
          this._ShipCountry = value;
          this.SendPropertyChanged("ShipCountry");
          this.OnShipCountryChanged();
        }
      }
    }

    [Association(Name="Order_Order_Detail", Storage="_Order_Details", OtherKey="OrderID")]
    public EntitySet<Order_Detail> Order_Details
    …{
      get
      …{
        return this._Order_Details;
      }
      set
      …{
        this._Order_Details.Assign(value);
      }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    …{
      if ((this.PropertyChanging != null))
      …{
        this.PropertyChanging(this, emptyChangingEventArgs);
      }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    …{
      if ((this.PropertyChanged != null))
      …{
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }

    private void attach_Order_Details(Order_Detail entity)
    …{
      this.SendPropertyChanging();
      entity.Order = this;
      this.SendPropertyChanged("Order_Details");
    }

    private void detach_Order_Details(Order_Detail entity)
    …{
      this.SendPropertyChanging();
      entity.Order = null;
      this.SendPropertyChanged("Order_Details");
    }
  }

  [Table(Name="dbo.[Order Details]")]
  public partial class Order_Detail : INotifyPropertyChanging, INotifyPropertyChanged
  …{

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

    private int _OrderID;

    private int _ProductID;

    private decimal _UnitPrice;

    private short _Quantity;

    private float _Discount;

    private EntityRef<Order> _Order;

  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 OnProductIDChanging(int value);
  partial void OnProductIDChanged();
  partial void OnUnitPriceChanging(decimal value);
  partial void OnUnitPriceChanged();
  partial void OnQuantityChanging(short value);
  partial void OnQuantityChanged();
  partial void OnDiscountChanging(float value);
  partial void OnDiscountChanged();
  #endregion

    public Order_Detail()
    …{
      OnCreated();
      this._Order = default(EntityRef<Order>);
    }

    [Column(Storage="_OrderID", DbType="Int NOT NULL", IsPrimaryKey=true)]
    public int OrderID
    …{
      get
      …{
        return this._OrderID;
      }
      set
      …{
        if ((this._OrderID != value))
        …{
          if (this._Order.HasLoadedOrAssignedValue)
          …{
            throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
          }
          this.OnOrderIDChanging(value);
          this.SendPropertyChanging();
          this._OrderID = value;
          this.SendPropertyChanged("OrderID");
          this.OnOrderIDChanged();
        }
      }
    }

    [Column(Storage="_ProductID", DbType="Int NOT NULL", IsPrimaryKey=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();
        }
      }
    }

    [Column(Storage="_UnitPrice", DbType="Money NOT NULL")]
    public decimal UnitPrice
    …{
      get
      …{
        return this._UnitPrice;
      }
      set
      …{
        if ((this._UnitPrice != value))
        …{
          this.OnUnitPriceChanging(value);
          this.SendPropertyChanging();
          this._UnitPrice = value;
          this.SendPropertyChanged("UnitPrice");
          this.OnUnitPriceChanged();
        }
      }
    }

    [Column(Storage="_Quantity", DbType="SmallInt NOT NULL")]
    public short Quantity
    …{
      get
      …{
        return this._Quantity;
      }
      set
      …{
        if ((this._Quantity != value))
        …{
          this.OnQuantityChanging(value);
          this.SendPropertyChanging();
          this._Quantity = value;
          this.SendPropertyChanged("Quantity");
          this.OnQuantityChanged();
        }
      }
    }

    [Column(Storage="_Discount", DbType="Real NOT NULL")]
    public float Discount
    …{
      get
      …{
        return this._Discount;
      }
      set
      …{
        if ((this._Discount != value))
        …{
          this.OnDiscountChanging(value);
          this.SendPropertyChanging();
          this._Discount = value;
          this.SendPropertyChanged("Discount");
          this.OnDiscountChanged();
        }
      }
    }

    [Association(Name="Order_Order_Detail", Storage="_Order", ThisKey="OrderID", IsForeignKey=true)]
    public Order Order
    …{
      get
      …{
        return this._Order.Entity;
      }
      set
      …{
        Order previousValue = this._Order.Entity;
        if (((previousValue != value)
              || (this._Order.HasLoadedOrAssignedValue == false)))
        …{
          this.SendPropertyChanging();
          if ((previousValue != null))
          …{
            this._Order.Entity = null;
            previousValue.Order_Details.Remove(this);
          }
          this._Order.Entity = value;
          if ((value != null))
          …{
            value.Order_Details.Add(this);
            this._OrderID = value.OrderID;
          }
          else
          …{
            this._OrderID = default(int);
          }
          this.SendPropertyChanged("Order");
        }
      }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    …{
      if ((this.PropertyChanging != null))
      …{
        this.PropertyChanging(this, emptyChangingEventArgs);
      }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    …{
      if ((this.PropertyChanged != null))
      …{
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }

本节介绍了O/R Designer的基本用法,下节讲一些比较高级的用法。O/R Designer相对与sqlmetal的好处就可以设计类的继承关系(下节再讲),而sqlmetal 相对于O/R Designer是可以一次性抽提所有表的信息,快速便捷。而且sqlmetal支持一些O/R Designer无法支持的信息提取。

时间: 2024-09-15 19:10:24

C# 3.0入门系列(七)--之OR工具介绍(下)的相关文章

C# 3.0入门系列-目录导航

1 C# 3.0 入门系列(一) 从linq开始引C# 3.0. 2 C# 3.0入门系列(二) 数据库的准备,引入Linq To Sql的准备. 3 C# 3.0入门系列(三) 第一个Linq To Sql工程. 4 C# 3.0入门系列(四)-之Select操作 Select语法. 5 C# 3.0入门系列(五)-之Where操作 Where操作语法. 6 C# 3.0入门系列(六)-之OrderBy操作 OrderBy 操作语法. 7 C# 3.0入门系列(七)--之OR工具介绍 C# 3

C# 3.0入门系列(十)-之Join操作

本节讲join操作.我们知道,T-sql中,有三种最基本的join,inner join, left join, 和right join. 而dlinq并不支持right join.道理很简单,right join以right表为基础,left表中没有对应记录的,将以null值填充.而dlinq以left表做为主表创建对象.如果一个对象为null,你如何获取它的其他的属性呢? 在C# 3.0入门系列(四)-之Select操作一文中,我们提到了query expression首先会被翻译成标准的A

C# 3.0入门系列(五)-之Where操作

从本节开始,本文正式更名为C#3.0入门系列.先发布一则消息,VS2007 Beta版本已经发布咯,下载地址: http://www.microsoft.com/downloads/details.aspx?FamilyID=1FF0B35D-0C4A-40B4-915A-5331E11C39E6&displaylang=en 大家快去下载呀,我也好和大家一起体验该版本最新功能呀. dlinq也更名为linq to sql.本文也跟着做相应变化,稍候,我会去更新前面的文章.我们先接着讲linq的

自定义View系列教程01--常用工具介绍

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Android多分辨率适配框架(3)- 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View

C# 3.0入门系列(七)--之OR工具介绍(上)

--不得不再次给关注dlinq的朋友道歉了.好久都没有更新blog了.因为工作的变动,还要赶期限,没有时间关注这里了. 先发布一则消息.Orcas Beta1, 这个才是beta1,可以到http://www.microsoft.com/downloads/details.aspx?FamilyID=f10fb5df-e369-4db4-b9a7-845dbf793368&DisplayLang=en 下载.5月1号的版本.最早4月19号就出来过一个,只是没有在意.还有一个http://www.

C# 3.0入门系列(十二)-Lambda表达式中Lifting

Lambda表达式是由匿名方法演化而来的更加高级的形式.关于匿名方法,请参阅http://msdn.microsoft.com/msdnmag/issues/04/05/C20/.关于Lambda表达式的演化,请参阅http://msdn.microsoft.com/msdnmag/issues/07/06/csharp30/default.aspx?loc=zh.英文原版为http://msdn.microsoft.com/msdnmag/issues/07/06/CSharp30/. 1,L

C# 3.0入门系列(十一)-之In, Like操作

有这么一个例子,寻找一个表中的某个字段介于某个给定的集合该怎么办?Sql写起来很简单,比如:Select * from table where id in (2,3, 4, 5). 就是寻找id字段为这个给定的集合(2,3, 4, 5)内的值.那Linq to Sql该怎么做呢?一个字,简单. In Operator 比如,我们想要查找,"AROUT", "BOLID" 和 "FISSA" 这三个客户的订单.该如何做呢?Linq to Sql是这

C# 3.0入门系列(九)-之GroupBy操作

有朋友反馈说我提供的sample不能编译.大概是版本的问题,可以到http://msdn2.microsoft.com/en-us/bb330936.aspx下载for beta1的版本.本节接着讲groupby. 上一节,我们讲了如何理解groupby返回的结果.本节会延这个思路阐述下去.先来看下面的例子 GroupBy操作中Select的匿名类 var q = from p in db.Products group p by p.CategoryID into g select new {

C# 3.0入门系列(四)-之Select操作

先给关注dlinq的朋友们道歉,最近工作实在忙,没有时间来写blog.从本节开始,我们讲dlinq语法咯.我们先从select子句讲起.看下面的例子. var q = from c in db.Customers select c.ContactName; 这是一个最简单的dlinq查询语句,查询得到联系人的名字.在这里,我需要提醒下大家的是,像这个语句只是个声明,dlinq并没有真正把数据取出来,只有当你需要该数据的时候,它才会帮你去取,这就是延迟加载(deferred loading).如果