.NET Core開發日誌——Dapper與MySQL
阿新 • • 發佈:2018-10-02
tin table 其它 constrain enum ado ODB spl 關系
Dapper作為.NET生態中廣為人知的輕量級ORM類庫在.NET Core裏仍能被有效利用,並且其不但可以連通SQL Server數據庫還提供對其它數據庫,比如MySQL的支持。這裏試驗了一下通過Dapper連接MySQL的方法。
MySQL
可以選擇直接安裝在原生系統中或是Docker裏。
Official
Docker
Table
在MySQL中建立兩張表。
city表:
CREATE TABLE `city` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` char(35) NOT NULL DEFAULT ‘‘, `CountryCode` char(3) NOT NULL DEFAULT ‘‘, `District` char(20) NOT NULL DEFAULT ‘‘, `Population` int(11) NOT NULL DEFAULT ‘0‘, PRIMARY KEY (`ID`), KEY `CountryCode` (`CountryCode`), CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`code`) ) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1
country表:
CREATE TABLE `country` ( `Code` char(3) NOT NULL DEFAULT ‘‘, `Name` char(52) NOT NULL DEFAULT ‘‘, `Continent` enum(‘Asia‘,‘Europe‘,‘North America‘,‘Africa‘,‘Oceania‘,‘Antarctica‘,‘South America‘) NOT NULL DEFAULT ‘Asia‘, `Region` char(26) NOT NULL DEFAULT ‘‘, `SurfaceArea` float(10,2) NOT NULL DEFAULT ‘0.00‘, `IndepYear` smallint(6) DEFAULT NULL, `Population` int(11) NOT NULL DEFAULT ‘0‘, `LifeExpectancy` float(3,1) DEFAULT NULL, `GNP` float(10,2) DEFAULT NULL, `GNPOld` float(10,2) DEFAULT NULL, `LocalName` char(45) NOT NULL DEFAULT ‘‘, `GovernmentForm` char(45) NOT NULL DEFAULT ‘‘, `HeadOfState` char(60) DEFAULT NULL, `Capital` int(11) DEFAULT NULL, `Code2` char(2) NOT NULL DEFAULT ‘‘, PRIMARY KEY (`Code`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
Package
應用程序工程中需要添加Dapper以及Mysql.Data類庫。
dotnet add package Dapper
dotnet add package MySql.Data
Entity
編寫兩個實體類,用於映射city與country表。
public class CityEntity { public int ID { get; set; } public string Name { get; set; } public string CountryCode { get; set; } public string District { get; set; } public int Population { get; set; } public CountryEntity Country { get; set; } public override string ToString() { return $"ID: {ID}, Name: {Name}, CountryCode: {CountryCode}, District: {District}, Population: {Population}, Country: {Country}"; } }
public class CountryEntity
{
public string Code { get; set; }
public string Name { get; set; }
public string Continent { get; set; }
public string Region { get; set; }
public decimal SurfaceArea { get; set; }
public int IndepYear { get; set; }
public int Population { get; set; }
public decimal LifeExpectancy { get; set; }
public decimal GNP { get; set; }
public decimal GNPOld { get; set; }
public string LocalName { get; set; }
public string GovernmentForm { get; set; }
public string HeadOfState { get; set; }
public int Capital { get; set; }
public string Code2 { get; set; }
public override string ToString()
{
return $"Code: {Code}, Name: {Name}, Continent: {Continent}, Region: {Region}, SurfaceArea: {SurfaceArea} ";
}
}
Repository
倉庫類中新加獲取10個城市數據的方法。這裏Dapper的Query方法有三個參數,第一個是需要執行的SQL語句,第二個是對象之間的對應關系(這個例子中city與country為一對一關系),並確定最終返回的對象類型,最後的SplitOn參數會告訴Dapper在結果集中兩張表之間以哪個字段進行分界。
public class CityRepository
{
public List<CityEntity> Get10Cities()
{
List<CityEntity> result;
using (var conn = new MySqlConnection("Host=localhost;Port=3306;Database=world;Uid=admin;pwd=admin"))
{
var sql = "SELECT * FROM city INNER JOIN country ON city.CountryCode = country.Code LIMIT 10";
result = conn.Query<CityEntity, CountryEntity, CityEntity>(sql,
(city, country) => { city.Country = country; return city; }, splitOn: "Code").ToList();
}
return result;
}
}
Test
static void Main(string[] args)
{
var repository = new CityRepository();
var cities = repository.Get10Cities();
cities.ForEach(e=>{
System.Console.WriteLine(e);
});
}
程序運行的結果如下,可以看到成功借助Dapper的力量從MySQL數據庫裏獲取了所需的數據。
.NET Core開發日誌——Dapper與MySQL