Part I This paper introduces the ELinq mapping configuration mode based on Attribute. This paper will introduce the mapping configuration mode based on fluent API.
This article still takes the customers table and Orders table of Northwind database as an example. Customers and Orders are one to many relationships.
1. Entity classes of customer and 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. Introduce the namespace of fluent API: using NLite.Data.Mapping.Fluent;
3. Create CustomerMap mapping class
class CustomerMap : ClassMap<Customer> { public CustomerMap() { //set up TableName mapping TableName("Customers"); // Set primary key mapping Id Property mapped to the CustomerId On column Id(e => e.Id).ColumnName("CustomerId"); //Column mapping Column(e => e.ContactName).ColumnName("ContactName"); Column(e => e.CompanyName); Column(e => e.City); Column(e => e.Country); //Phone Fields ignore mapping Ignore(e => e.Phone); } }
4. Create OrderMap mapping class
class OrderMap : ClassMap<Order> { public OrderMap() { TableName("Orders"); //Set the primary key mapping and set it to add one automatically Id(e => e.OrderID).DbGenerated(); Column(e => e.CustomerID); Column(e => e.OrderDate); } }
5. Establish one to many relationship mapping
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() { //set up TableName mapping TableName("Customers"); // Set primary key mapping Id Property mapped to the CustomerId On column Id(e => e.Id).ColumnName("CustomerId"); //Column mapping Column(e => e.ContactName).ColumnName("ContactName"); Column(e => e.CompanyName); Column(e => e.City); Column(e => e.Country); //Phone Fields ignore mapping Ignore(e => e.Phone); //One to many mapping Association(e => e.Orders).ThisKey("Id").OtherKey("CustmerId"); } }
6. Establish many to one mapping
public class Order { public int OrderID; public string CustomerID; public DateTime OrderDate; public Customer Customer; } class OrderMap : ClassMap<Order> { public OrderMap() { TableName("Orders"); //Set the primary key mapping and set it to add one automatically Id(e => e.OrderID).DbGenerated(); Column(e => e.CustomerID); Column(e => e.OrderDate); //Many to one mapping Association(e => e.CustomerID).ThisKey("CustomerID").OtherKey("Id"); } }
7. Register the ClassMap of Fluent to the DbConfiguration object, and establish the DbContext
public class Northwind : DbContext { //Connection string Name: Based on Config Configuration of connection strings in files const string connectionStringName = "Northwind"; //structure dbConfiguration object 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. The previous descriptions are all based on strongly typed ClassMap. ELinq also supports anonymous ClassMap mapping and registration
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 { //Connection string Name: Based on Config Configuration of connection strings in files const string connectionStringName = "Northwind"; //structure dbConfiguration object static DbConfiguration dbConfiguration = DbConfiguration .Configure(connectionStringName) .SetSqlLogger(() => new SqlLog(Console.Out)) .AddClass<Customer>(m=> { //set up TableName mapping m.TableName("Customers"); // Set primary key mapping Id Property mapped to the CustomerId On column m.Id(e => e.Id).ColumnName("CustomerId"); //Column mapping m.Column(e => e.ContactName).ColumnName("ContactName"); m.Column(e => e.CompanyName); m.Column(e => e.City); m.Column(e => e.Country); //Phone Fields ignore mapping m.Ignore(e => e.Phone); //One to many mapping m.Association(e => e.Orders).ThisKey("Id").OtherKey("CustmerId"); }) .AddClass<Order>(m=> { m.TableName("Orders"); //Set the primary key mapping and set it to add one automatically m.Id(e => e.OrderID).DbGenerated(); m.Column(e => e.CustomerID); m.Column(e => e.OrderDate); //Many to one mapping m.Association(e => e.CustomerID).ThisKey("CustomerID").OtherKey("Id"); }) ; public Northwind() : base(dbConfiguration) { } public readonly IDbSet<Customer> Customers; public readonly IDbSet<Order> Orders; }
Through the above example, I think you have a better understanding of ELinq's configuration mode based on fluent API. Fluent mode is similar to Attribute mode, with cleaner entity and simpler code.
The next section describes the mapping configuration based on XML.
Technical support:
- Official website
- Nuge download page
- ORM components ELinq series
- ORM component ELinq update log
- Using the ORM component ELinq to answer questions
- In my blog message, I will try my best to take time to answer your questions.
- Join the QQ group of ELinq users (271342583).
Thank you for reading, please point to recommend, thank you again. C