關(guān)注.Net框架
今年6月微軟發(fā)布了.net core1.0,老實說我一直認(rèn)為.net已經(jīng)沒落了,最近由于公司想搭建mvc平臺,自己的golang方案被斃了才會又關(guān)注到.net平臺。此時我由衷地要鼓吹微軟一番,微軟一直是坑的代表,然而要么不發(fā)力,一發(fā)力就是大招。比如vscode,也不是針對誰了,在座的editor甚至ide都是垃圾。.net core暫時我們還不能一瞰全貌,但是至少我們可以跑個分xi

運行環(huán)境
- 系統(tǒng):MacOS
- 開發(fā)工具:Visual studio code
- 依賴擴展:C#、Nuget
開始Hello World
dotnet new
dotnet restore
dotnet run
這個當(dāng)然是 Hello World!
也可以通過dotnet new -t web 命令直接構(gòu)建一個asp.net core程序,本文的舉例是普通的helloworld舉例,增加了mysql的連接。
mysql操作示例
1、project.json配置:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"MySql.Data.Core": "7.0.4-IR-191",
"MySql.Data.EntityFrameworkCore": "7.0.4-ir-191"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}
如上所示,除了dependencies的內(nèi)容,都是自動生成的,而mysql的引用是通過Nuget引入的,Nuget是一個優(yōu)秀的包管理工具,通過Nuget添加引用,添加的引用都會體現(xiàn)在project.json文件中,然后使用dotnet restore命令另引用生效。
2、增加appsettings.json文件
在appsettings中添加
{
"ConnectionStrings":
{
"SampleConnection": "server=localhost;userid=用戶;pwd=密碼;port=端口;database=數(shù)據(jù)庫;sslmode=none;"
}
}
這里請注意,sslmode=none絕對不能漏,查到的好多教程用的mysql包都是第三方而不是官方的,問題就在于他們沒有加入sslmode的設(shè)定
3、增加文件EmployeesContext.cs這里用的是官網(wǎng)介紹的例子,在mysql中定義了Employees表,包含Id,name,lastname三個字段,id為key且自增
namespace ConsoleApplication
{
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
///
/// The entity framework context with a Employees DbSet
///
public class EmployeesContext : DbContext
{
public EmployeesContext(DbContextOptions options)
: base(options)
{ }
public DbSet Employees { get;set;}
}
///
/// Factory class for EmployeesContext
///
public static class EmployeesContextFactory
{
public static EmployeesContext Create(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseMySQL(connectionString);
//Ensure database creation
var context = new EmployeesContext(optionsBuilder.Options);
context.Database.EnsureCreated();
return context;
}
}
///
/// A basic class for an Employee
///
public class Employee
{
public Employee()
{
}
public int Id { get;set;}
[MaxLength(30)]
public string Name { get;set;}
[MaxLength(50)]
public string LastName { get;set;}
}
}
4、編輯Program.cs文件
在Main函數(shù)中加入代碼
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var configuration = builder.Build();
string connectionString = configuration.GetConnectionString("SampleConnection");
// Create an employee instance and save the entity to the database
var entry = new Employee() { Name = "Beta", LastName = "Fun" };
using (var context = EmployeesContextFactory.Create(connectionString))
{
var emp = context.Employees.Where(e => e.Id == 1).FirstOrDefault();
Console.WriteLine($"我查到了{(lán)emp.Name}");
context.Add(entry);
context.SaveChanges();
}
Console.WriteLine($"Employee was saved in the database with id: {entry.Id}");
這段代碼仍然是我從官網(wǎng)上搬運過來的,中間加了一點測試方法,數(shù)據(jù)庫訪問用的entityframework core ,與以前的entityframework差異不大,過渡是比較平滑的。
來說說坑
- appsettings.json 是我手動加的,vscode編譯的時不會自動生成到bin文件夾下面,貌似是沒有依賴的問題,調(diào)試連接的過程中,我只能一次次手工復(fù)制到bin文件中去 (主要還是因為我把mysql的密碼忘了)
- oracle總是讓人覺得厭煩,驅(qū)動總是最晚,部署永遠(yuǎn)最麻煩

- DataTable 作為曾經(jīng)最強大的類之一,當(dāng)我想用他的時候,結(jié)果竟然是這樣的
