.Net Core 初探

關(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)最麻煩
WechatIMG1.jpeg
  • DataTable 作為曾經(jīng)最強大的類之一,當(dāng)我想用他的時候,結(jié)果竟然是這樣的
WechatIMG2.jpeg
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • .net跨平臺前世今生 .NET core 1.0并不是對原有的.net平臺的升級,而是一次全新的重寫,這個開發(fā)過...
    never_say_never閱讀 10,808評論 1 6
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 2015年7月30日 本文作者是 Managed Languages 團隊項目經(jīng)理 Lucian Wischik。...
    OneAPM閱讀 2,853評論 1 5
  • Haskell特性 簡潔的程序風(fēng)格 強大的類型系統(tǒng) List comprehensions(wiki的意思就是有基...
    du1dume閱讀 3,524評論 0 1
  • 大家都說大學(xué)的戀愛是單純美好的,都說轟轟烈烈愛一場之后我們可以轉(zhuǎn)身過自己的人生,都說找相愛的人戀愛,找適合的...
    3M閱讀 357評論 0 1

友情鏈接更多精彩內(nèi)容