.NET Coredotnet core

.Net Core 初探

2016-10-26  本文已影响152人  BetaFun

关注.Net框架

今年6月微软发布了.net core1.0,老实说我一直认为.net已经没落了,最近由于公司想搭建mvc平台,自己的golang方案被毙了才会又关注到.net平台。此时我由衷地要鼓吹微软一番,微软一直是坑的代表,然而要么不发力,一发力就是大招。比如vscode,也不是针对谁了,在座的editor甚至ide都是垃圾。.net core暂时我们还不能一瞰全貌,但是至少我们可以跑个分xi


运行环境

开始Hello World

dotnet new
dotnet restore
dotnet run

这个当然是 Hello World!
也可以通过dotnet new -t web 命令直接构建一个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的内容,都是自动生成的,而mysql的引用是通过Nuget引入的,Nuget是一个优秀的包管理工具,通过Nuget添加引用,添加的引用都会体现在project.json文件中,然后使用dotnet restore命令另引用生效。

2、增加appsettings.json文件
在appsettings中添加

{
"ConnectionStrings":
{
"SampleConnection": "server=localhost;userid=用户;pwd=密码;port=端口;database=数据库;sslmode=none;"
}
}

这里请注意,sslmode=none绝对不能漏,查到的好多教程用的mysql包都是第三方而不是官方的,问题就在于他们没有加入sslmode的设定
3、增加文件EmployeesContext.cs这里用的是官网介绍的例子,在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函数中加入代码

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($"我查到了{emp.Name}");
context.Add(entry);
context.SaveChanges();
}
Console.WriteLine($"Employee was saved in the database with id: {entry.Id}");

这段代码仍然是我从官网上搬运过来的,中间加了一点测试方法,数据库访问用的entityframework core ,与以前的entityframework差异不大,过渡是比较平滑的。


来说说坑

WechatIMG1.jpeg WechatIMG2.jpeg
上一篇 下一篇

猜你喜欢

热点阅读