上篇介绍了基于Attribute的ELinq映射配置方式,本篇将给大家介绍基于FluentAPI的映射配置方式。
该篇仍然以Northwind数据库的Customers表和Orders 表为例,客户和订单是一对多关系。
1. Customer和Order的实体类
public class Customer { public string Id; public string ContactName; public string CompanyName; public string City; public string Country; public string Phone; } public class Order { public int OrderID; public string CustomerID; public DateTime OrderDate; }
2. 引入FluentAPI的命名空间:using NLite.Data.Mapping.Fluent;
3. 创建CustomerMap 映射类
class CustomerMap : ClassMap<Customer> { public CustomerMap() { //设置TableName映射 TableName("Customers"); // 设置主键映射,把Id属性映射到数据表的CustomerId列上 Id(e => e.Id).ColumnName("CustomerId"); //列映射 Column(e => e.ContactName).ColumnName("ContactName"); Column(e => e.CompanyName); Column(e => e.City); Column(e => e.Country); //Phone 字段忽略映射 Ignore(e => e.Phone); } }
4. 创建OrderMap映射类
class OrderMap : ClassMap<Order> { public OrderMap() { TableName("Orders"); //设置主键映射,并且设置为自动增一的 Id(e => e.OrderID).DbGenerated(); Column(e => e.CustomerID); Column(e => e.OrderDate); } }
5. 建立一对多关系映射
public class Customer { public string Id; public string ContactName; public string CompanyName; public string City; public string Country; public string Phone; public IList<Order> Orders; } class CustomerMap : ClassMap<Customer> { public CustomerMap() { //设置TableName映射 TableName("Customers"); // 设置主键映射,把Id属性映射到数据表的CustomerId列上 Id(e => e.Id).ColumnName("CustomerId"); //列映射 Column(e => e.ContactName).ColumnName("ContactName"); Column(e => e.CompanyName); Column(e => e.City); Column(e => e.Country); //Phone 字段忽略映射 Ignore(e => e.Phone); //一对多映射 Association(e => e.Orders).ThisKey("Id").OtherKey("CustmerId"); } }
6. 建立多对一映射
public class Order { public int OrderID; public string CustomerID; public DateTime OrderDate; public Customer Customer; } class OrderMap : ClassMap<Order> { public OrderMap() { TableName("Orders"); //设置主键映射,并且设置为自动增一的 Id(e => e.OrderID).DbGenerated(); Column(e => e.CustomerID); Column(e => e.OrderDate); //多对一映射 Association(e => e.CustomerID).ThisKey("CustomerID").OtherKey("Id"); } }
7. 注册Fluent的ClassMap到DbConfiguration对象中,并建立DbContext
public class Northwind : DbContext { //连接字符串名称:基于Config文件中连接字符串的配置 const string connectionStringName = "Northwind"; //构造dbConfiguration 对象 static DbConfiguration dbConfiguration = DbConfiguration .Configure(connectionStringName) .SetSqlLogger(() => new SqlLog(Console.Out)) .AddClass(new CustomerMap()) .AddClass(new OrderMap()) ; public Northwind() : base(dbConfiguration) { } public readonly IDbSet<Customer> Customers; public readonly IDbSet<Order> Orders; }
8. 前面介绍的都是基于强类型的ClassMap,ELinq也支持匿名ClassMap的映射和注册
public class Customer { public string Id; public string ContactName; public string CompanyName; public string City; public string Country; public string Phone; public IList<Order> Orders; } public class Order { public int OrderID; public string CustomerID; public DateTime OrderDate; public Customer Customer; } public class Northwind : DbContext { //连接字符串名称:基于Config文件中连接字符串的配置 const string connectionStringName = "Northwind"; //构造dbConfiguration 对象 static DbConfiguration dbConfiguration = DbConfiguration .Configure(connectionStringName) .SetSqlLogger(() => new SqlLog(Console.Out)) .AddClass<Customer>(m=> { //设置TableName映射 m.TableName("Customers"); // 设置主键映射,把Id属性映射到数据表的CustomerId列上 m.Id(e => e.Id).ColumnName("CustomerId"); //列映射 m.Column(e => e.ContactName).ColumnName("ContactName"); m.Column(e => e.CompanyName); m.Column(e => e.City); m.Column(e => e.Country); //Phone 字段忽略映射 m.Ignore(e => e.Phone); //一对多映射 m.Association(e => e.Orders).ThisKey("Id").OtherKey("CustmerId"); }) .AddClass<Order>(m=> { m.TableName("Orders"); //设置主键映射,并且设置为自动增一的 m.Id(e => e.OrderID).DbGenerated(); m.Column(e => e.CustomerID); m.Column(e => e.OrderDate); //多对一映射 m.Association(e => e.CustomerID).ThisKey("CustomerID").OtherKey("Id"); }) ; public Northwind() : base(dbConfiguration) { } public readonly IDbSet<Customer> Customers; public readonly IDbSet<Order> Orders; }
通过上面的例子想必大家对ELinq基于FluentAPI的配置方式也有了一个了解。Fluent方式和Attribute方式相似,Fluent方式实体更干净,Attribute方式代码更简洁。
下一节将介绍基于XML方式的映射配置。
技术支持:
谢谢大家的阅读,麻烦大伙点一下推荐,再次谢谢大家。 ^_^