• 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的值必须是数据库表存在的字段名。

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

  • 相关阅读:
    Using Resource File on DotNet
    C++/CLI VS CSharp
    JIT VS NGen
    [Tip: disable vc intellisense]VS2008 VC Intelisense issue
    UVa 10891 Game of Sum(经典博弈区间DP)
    UVa 10723 Cyborg Genes(LCS变种)
    UVa 607 Scheduling Lectures(简单DP)
    UVa 10401 Injured Queen Problem(简单DP)
    UVa 10313 Pay the Price(类似数字分解DP)
    UVa 10635 Prince and Princess(LCS N*logN)
  • 原文地址:https://www.cnblogs.com/htht66/p/2306803.html
Copyright © 2020-2023  润新知