zoukankan      html  css  js  c++  java
  • 04创建实体模型

    在使用LINQ to SQL之前,除了要属性LINQ的语法,还要创建实体模型。

    1 创建Project

    新建控制台项目,命名为LinqConsoleApp

    2 添加DLLNamepace

    1.添加System.Data.Linq.dll

    2.using System.Data.Linq;

    3.using System.Data.Linq.Mapping;

    3 将类映射到数据库表

    以NorthWind为例,建立Customer和Order实体及其联系。

    3.1创建Order

    [Table(Name = "Orders")]
        public class Order
        {
            private int _OrderID = 0;
            private string _CustomerID;
            private EntityRef<Customer> _Customer;
    
            public Order()
            {
                this._Customer = new EntityRef<Customer>();
            }
    
            
            /// <summary>
            /// OrderID 属性
            /// </summary>
            /// <remarks>
            /// DbType:数据表字段的数据类型
            /// IsDbGenerated:是否自动增长
            /// </remarks>
            [Column(Storage = "_OrderID", DbType = "Int NOT NULL IDENTITY",
            IsPrimaryKey = true, IsDbGenerated = true)]
            public int OrderID
            {
                get { return this._OrderID; }
            }
    
            [Column(Storage = "_CustomerID", DbType = "NChar(5)")]
            public string CustomerID
            {
                get { return this._CustomerID; }
                set { this._CustomerID = value; }
            }
    
    
            //------------ 定义联系 ------------------------       
            /// <summary>
            /// 导航到 Customer
            /// </summary>
            /// <remarks>
            /// 使用 CustomerID 关联到 Customer ThisKey
            /// Order的外键(CustomerID) 在 Customer 是主键,所以使用 ThisKey
            /// </remarks>
            [Association(Storage = "_Customer", ThisKey = "CustomerID")]
            public Customer Customer
            {
                get { return this._Customer.Entity; }
                set { this._Customer.Entity = value; }
            }
        }

    3.2创建Customer

     /// <summary>
        /// 客戶類
        /// </summary>
        /// <remarks>
        /// Table的Name屬性指定對應的數據庫表名
        /// </remarks>
        [Table(Name = "Customers")]
        public class Customer
        {
            private string _CustomerID;
    
            /// <summary>
            /// 類成員之屬性對應到數據庫表的字段
            /// 默認使用屬性,可以使用Name指定,並且Name的值必須是數據庫表存在的字段名。
            /// IsPrimaryKey:是否為主鍵
            /// Storage:儲存值的類成員之字段
            /// </summary>
            [Column(IsPrimaryKey = true, Storage = "_CustomerID", Name = "CustomerID")]
            public string CustomerID
            {
                get
                {
                    return this._CustomerID;
                }
                set
                {
                    this._CustomerID = value;
                }
    
            }
    
            private string _City;
            [Column(Storage = "_City")]
            public string City
            {
                get
                {
                    return this._City;
                }
                set
                {
                    this._City = value;
                }
            }
    
            //------------ 定義聯繫 ------------------------
            //客戶有多個訂單,所以使用Set
            private EntitySet<Order> _Orders;
            public Customer()
            {
                this._Orders = new EntitySet<Order>();
            }
    
            /// <summary>
            /// 客戶導航到訂單
            /// </summary>
            /// <remarks>
            /// 使用 CustomerID 關係到 Orders OtherKey
            /// Orders 為多端,所以使用 OtherKey
            /// Order 用到的外鍵:CustomerID
            /// </remarks>
            [Association(Storage = "_Orders", OtherKey = "CustomerID")]
            public EntitySet<Order> Orders
            {
                get { return this._Orders; }
                set { this._Orders.Assign(value); }
            }
        }

    注意:默认使用属性,可以使用Name指定,并且Name的值必须是数据库表存在的字段名。

    方法二、使用工具,命令行/图形设计工具

  • 相关阅读:
    JavaScript 正则表达式上——基本语法
    温故知新 javascript 正则表达式
    Js把IE COM数组列表转换成数组
    Linux用户和用户组管理总结
    Linux FTP 服务器配置简单说明
    Linux中Samba详细安装
    js本地存储解决方案(localStorage与userData)
    通过了解渲染过程来提高页面性能
    使用Chrome DevTools的Timeline和Profiles提高Web应用程序的性能
    内存泄漏,循环引用
  • 原文地址:https://www.cnblogs.com/htht66/p/2306803.html
Copyright © 2011-2022 走看看