★ 简介 

    该文档描述了如何采用手动的方式映射你的对象类到数据表(而不是使用像SqlMetal这样的自动化工具)以便能够支持数据表之间的M:M关系和使用实体类的数据绑定。 

该实例以著名的Northwind数据库为例,数据库中包含表Customers,Orders,Order Details,Products。每个产品仅仅属性一个产品类别,但是每个产品可以有多个订单,一个订单可以有多个顾客。Order Detail表用于处理Orders表和Products表之间多对多的关系。为了简单起见,除了主键之外,其余的字段都为空。

这些表可以允许我们针对每个主类型进行关系(1:M,M:1,和M:M)之间的映射。

1. 在Nuget 控制台中输入下面的命令行

  Install-Package NLite.Data

2. 添加Northwind.cs 文件      

using NLite.Data.Linq;
using NLite.Data.Mapping; 
namespace  NLite.Data.Test.Model.Northwind
{ 

}

   3. 定义实体类以及建立映射关系

 [Table(Name = "Customers")]
    public class Customer
    {
        [Id]
        public string CustomerID;
        [Column]
        public string ContactName;
        [Column]
        public string CompanyName;
        [Column]
        public string Phone;
        [Column]
        public string City;
        [Column]
        public string Country;
        [Association]
        public IList<Order> Orders;
    }

    [Table(Name = "Orders")]
    public class Order
    {
        [Id(IsDbGenerated = true)]
        public int OrderID;
        [Column]
        public string CustomerID;
        [Column]
        public DateTime OrderDate;
        [Association]
        public Customer Customer;
        [Association]
        public List<OrderDetail> Details;
    }

    [Table(Name = "Order Details")]
    public class OrderDetail
    {
        [Id]
        public int? OrderID { get; set; }
        [Id]
        public int ProductID { get; set; }
        [Association]
        public Product Product;
    }

      [Table(Name = "Products")]
    public class Product : IEntity
    {
        [Id]
        public int ID;
        [Column]
        public string ProductName;
        [Column]
        public bool Discontinued;

    }

4.   建立DbContext

 
public class Northwind
    {
        
        const string connectionStringName = "Northwind";

        static DbConfiguration dbConfiguration = DbConfiguration
                .Configure(connectionStringName)
                .SetSqlLogger(() => new SqlLog(Console.Out))
                .AddClass<Customer>()
                .AddClass<Order>()
                .AddClass<OrderDetail>()
                .AddClass<Product>();

        private IDbContext dbContext;
        public Northwind()
        {
            this.dbContext = dbConfiguration.CreateDbContext();
        }
        public virtual IDbSet<Customer> Customers
        {
            get { return this.dbContext.Set<Customer>(); }
        }
        public virtual IDbSet<Order> Orders
        {
            get { return this.dbContext.Set<Order>(); }
        }
        public virtual IDbSet<OrderDetail> OrderDetails
        {
            get { return this.dbContext.Set<OrderDetail>(); }
        }
        public virtual IDbSet<Product> Products
        {
            get { return this.dbContext.Set<Product>(); }
        }
    }
  

5. 配置连接字符串

 

<configuration>
  <connectionStrings>
    <clear/>
        <add providerName="System.Data.SqlClient" name="Northwind" connectionString="server=.\\SQLEXPRESS;database=Northwind;uid=sa;pwd=password;"/>
  </connectionStrings>
</configuration>


 

Last edited Nov 19, 2012 at 1:51 AM by qswang, version 8

Comments

No comments yet.