การใช้งาน Entity Framework กับ MySQL Database (LINQ to Entities - MySQL Database) |
การใช้งาน Entity Framework กับ MySQL Database (LINQ to Entities - MySQL Database) ความสามารถของ Entity Framework ได้พัฒนาให้มากกว่าที่จะรองรับเฉพาะ Database ของ SQL Server เท่านั้น ซึ่งเราสามารถใช้งานกับมันได้ในหลาย ๆ Database และในหัวข้อนี้เราจะมาเรียนรู้วิธีการ Config และติดตั้งรายการ Driver ต่าง ๆ เพื่อให้สามารถใช้ MySQL Database กับ Entity Framework ได้ ซึ่งในการใช้งานจะรองรับตั้งแต่ Entity Framework 5 ขึ้นไป และอาจจะต้องมำการติดตั้งพวก Driver ต่าง ๆ ที่เกี่ยวข้องด้วย
Table : customer
CREATE TABLE IF NOT EXISTS `customer` (
`CUSTOMER_ID` varchar(4) NOT NULL,
`NAME` varchar(50) NOT NULL,
`EMAIL` varchar(50) NOT NULL,
`COUNTRY_CODE` varchar(2) NOT NULL,
`BUDGET` decimal(18,2) NOT NULL,
`USED` decimal(18,2) NOT NULL,
PRIMARY KEY (`CUSTOMER_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `customer`
--
INSERT INTO `customer` (`CUSTOMER_ID`, `NAME`, `EMAIL`, `COUNTRY_CODE`, `BUDGET`, `USED`) VALUES
('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000.00, 600000.00),
('C002', 'John Smith', '[email protected]', 'UK', 2000000.00, 800000.00),
('C003', 'Jame Born', '[email protected]', 'US', 3000000.00, 600000.00),
('C004', 'Chalee Angel', '[email protected]', 'US', 4000000.00, 100000.00);

Table : country
CREATE TABLE IF NOT EXISTS `country` (
`COUNTRY_CODE` varchar(2) NOT NULL,
`COUNTRY_NAME` varchar(50) NOT NULL,
PRIMARY KEY (`COUNTRY_CODE`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `country`
--
INSERT INTO `country` (`COUNTRY_CODE`, `COUNTRY_NAME`) VALUES
('TH', 'Thailand '),
('UK', 'United Kingdom'),
('US', 'United States');

Table : audit
CREATE TABLE IF NOT EXISTS `audit` (
`AUDIT_ID` int(4) NOT NULL,
`CUSTOMER_ID` varchar(4) NOT NULL,
`LOG_DATE` date NOT NULL,
`USED` decimal(18,2) NOT NULL,
PRIMARY KEY (`AUDIT_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `audit`
--
INSERT INTO `audit` (`AUDIT_ID`, `CUSTOMER_ID`, `LOG_DATE`, `USED`) VALUES
(1, 'C001', '2015-08-01', 100000.00),
(2, 'C001', '2015-08-05', 200000.00),
(3, 'C001', '2015-08-10', 300000.00),
(4, 'C002', '2015-08-02', 400000.00),
(5, 'C002', '2015-08-07', 100000.00),
(6, 'C002', '2015-08-15', 300000.00),
(7, 'C003', '2015-08-20', 400000.00),
(8, 'C003', '2015-08-25', 200000.00),
(9, 'C004', '2015-07-04', 100000.00);

เปิดโปรเจคบน Visual Studio
สิ่งที่จำเป็นต้องมี (ให้ดาวน์โหลดมาติดตั้งให้ครบ)- MySQL Connector/Net 6.8.x
- MySQL Server 5.1 or above
- Entity Framework 5 or 6 assemblies
- .NET Framework 4.0 or above
- MySQL for Visual Studio 1.2.3 GA

เลือก Add รายการ ADO.NET Entity Data Model

EF Designer from Database

เลือก New Connection

เลือก Change

จะเห็นว่าตอนนี้ยังไม่มีรายการของ MySQL Driver
ให้ทำการติดตั้งรายการ Driver ต่าง ๆ ที่ได้แจ้งไว้ก่อนหน้านี้

MySQL Connector/Net 6.8.x

Entity Framework 5 or 6 assemblies

MySQL for Visual Studio 1.2.3 GA

จากนั้นให้เลือก Add จะเห็นว่ามี Driver ของ MySQL Database

กรอกข้อมูลในการเชื่อมต่อกับ MySQL Database

เลือกรายการ Table ที่ต้องการเพ่มลงใน Entity Model

รายการ Table บน Entity Model

ทดสอบสร้าง DataGridView สำหรับการทดสอบเรียกข้อมูล
Code C#
// Create new entities Object
using (var db = new myDatabaseEntities())
{
// Get data from customer
var ds = (from c in db.customer
select c).ToList();
// Assign to DataGridView
if (ds.Count() > 0)
{
this.myDataGridView.DataSource = ds;
}
}
Code VB.Net
' Create new entities Object
Using db = New myDatabaseEntities()
' Get data from customer
Dim ds = (From c In db.customer Select c).ToList()
' Assign to DataGridView
If ds.Count() > 0 Then
Me.myDataGridView.DataSource = ds
End If
End Using
Result

สำหรับวิฌีการใช้งานอื่น ๆ ไม่ได้ต่างกับการใช้ Entity Framework ด้วย SQL Database
|