NET Core 2.1 SDK 或以上版本
2)创建新项目(通过命令行创建项目)
“工具” -> “NuGet包管理器” -> “程序包管理器控制台”
新建控制台项目:
dotnet new console -o ConsoleApp_Efcode
3)更改当前目录
在后续步骤中,我们需要针对应用程序发出 dotnet 命令。
我们将当前目录更改为应用程序的目录,如下所示:
cd ConsoleApp_Efcode/
4)安装EntityFrameworkCore以及使用到的依赖
dotnet add package Microsoft.EntityFrameworkCore.Design -v 2.2.4
dotnet add package System.Runtime.CompilerServices.Unsafe -v 4.5.2
打开项目ConsoleApp_Efcod,会看到“依赖项”->“Nuget”下有如下引用:
添加Kdbndp.EntityFrameworkCore.KingbaseES.dll,Kdbndp.dll的引用:“依赖项”->“添加引用”添加Kdbndp.dll与Kdbndp.EntityFrameworkCore.KingbaseES.dll
5)建模型
定义构成模型的上下文和实体类:
创建一个新的 Model.cs 文件添加使用以下内容:
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace ConsoleApp_Efcode
{
public enum State
{
None,
有用,
无用
}
public class Modules : DbContext
{
public DbSet Kdb_Blog_Tests { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseKdbndp("Server=192.168.28.212;User ID=SYSTEM;Password=MANAGER;Database=KDBNDP_TESTS;Port=54328");
}
public class Blog_Test
{
[Key]
public Guid Id { get; set; }
public Guid? Ids { get; set; }
public string Name { get; set; }
public bool Sex { get; set; }
public bool? Sexy { get; set; }
public int Age { get; set; }
public int? Ager { get; set; }
public DateTime Birth { get; set; }
public DateTime? Birthy { get; set; }
public float Money { get; set; }
public float? Moneies { get; set; }
public double Pi { get; set; }
public double? Pis { get; set; }
public State State { get; set; }
public State? States { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
}
提示:在实际应用程序中,将每个类放在单独的文件中,并将连接字符串放在配置文件或环境变量中。为简化本教程,所有内容均放在一个文件中。
6)创建数据库
有了模型后,即可通过迁移创建数据库,运行以下命令为迁移搭建基架,并为模型创建一组初始表:
dotnet ef migrations add InitialCreate
命令执行成功以后,会生成如下文件:
如果执行命令时提示:
Build failed.
检查程序中模型创建或程序中是否有错误。
或者提示:
dotnet ef不存在,可参考微软官方文档,地址如下:https://docs.microsoft.com/zh-tw/ef/core/miscellaneous/cli/dotnet
dotnet tool install --global dotnet-ef --version 3.0.0-*
运行以下命令将新迁移应用到数据库。 在应用迁移之前,此命令可创建数据库。
dotnet ef database update
执行成功会显示如下信息:
查看数据库中是否创建对应模型对象的表->“Kdb_Blog_Tests”;
7)使用模型
using System;
namespace ConsoleApp_Efcode
{
public class Program
{
// Kdbndp_efcode测试用例
static void TestKdbndp_efcode()
{
using (var db = new Modules())
{
// add data
db.Kdb_Blog_Tests.Add(new Blog_Test
{
Id = Guid.NewGuid(),
Ids = Guid.NewGuid(),
Name = “刘备”,
Sex = true,
Sexy = true,
Age = 45,
Ager = 45,
Birth = DateTime.Now,
Birthy = DateTime.Now,
Money = 1.5f,
Moneies = 1.5f,
Pi = 36.25,
Pis = 36.25,
State = State.无用,
States = State.有用
});
var count = db.SaveChanges();
Console.WriteLine("{0} records saved to database", count);
Console.WriteLine(“All blogs in database:”);
//search data
foreach (var blog in db.Kdb_Blog_Tests)
{
Console.WriteLine("Id: {0}", blog.Id);
Console.WriteLine("Ids: {0}", blog.Ids);
Console.WriteLine("Name: {0}", blog.Name);
Console.WriteLine("Sex: {0}", blog.Sex);
Console.WriteLine("Sexy: {0}", blog.Sexy);
Console.WriteLine("Age: {0}", blog.Age);
Console.WriteLine("Ager: {0}", blog.Ager);
Console.WriteLine("Birth: {0}", blog.Birth);
Console.WriteLine("Birthy: {0}", blog.Birthy);
Console.WriteLine("Money: {0}", blog.Money);
Console.WriteLine("Birthy: {0}", blog.Birthy);
Console.WriteLine("Money: {0}", blog.Money);
Console.WriteLine("Moneies: {0}", blog.Moneies);
Console.WriteLine("Pi: {0}", blog.Pi);
Console.WriteLine("Pis: {0}", blog.Pis);
Console.WriteLine("State: {0}", blog.State);
Console.WriteLine("States: {0}", blog.States);
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
TestKdbndp_efcode();
Console.ReadKey();
}
}
}
8)从控制台测试应用
dotnet run
备注:本例提供了Kdbndp.EntityFrameworkCore.KingbaseES的简单测试用例。
namespace Kdbndp_net_framework
{
public enum State
{
None,
有用,
无用
}
public class Modules : DbContext
{
public DbSet Kdb_Blog_Tests { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseKdbndp("Server=192.168.28.212;User ID=SYSTEM;Password=MANAGER;Database=KDBNDP_TESTS;Port=54328");
}
public class Blog_Test
{
[Key]
public Guid Id { get; set; }
public Guid? Ids { get; set; }
public string Name { get; set; }
public bool Sex { get; set; }
public bool? Sexy { get; set; }
public int Age { get; set; }
public int? Ager { get; set; }
public DateTime Birth { get; set; }
public DateTime? Birthy { get; set; }
public float Money { get; set; }
public float? Moneies { get; set; }
public double Pi { get; set; }
public double? Pis { get; set; }
public State State { get; set; }
public State? States { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
}
5) 创建数据库
namespace Kdbndp_net_framework
{
class Program
{
// Kdbndp _efcode测试用例
static void TestKdbndp_efcode()
{
using (var db = new Modules())
{
// add data
db.Kdb_Blog_Tests.Add(new Blog_Test
{
Id = Guid.NewGuid(),
Ids = Guid.NewGuid(),
Name = “刘备”,
Sex = true,
Sexy = true,
Age = 45,
Ager = 45,
Birth = DateTime.Now,
Birthy = DateTime.Now,
Money = 1.5f,
Moneies = 1.5f,
Pi = 36.25,
Pis = 36.25,
State = State.无用,
States = State.有用
});
var count = db.SaveChanges();
Console.WriteLine("{0} records saved to database", count);
Console.WriteLine("All blogs in database:");
//search
foreach (var blog in db.Kdb_Blog_Tests)
{
Console.WriteLine("Id: {0}", blog.Id);
Console.WriteLine("Ids: {0}", blog.Ids);
Console.WriteLine("Name: {0}", blog.Name);
Console.WriteLine("Sex: {0}", blog.Sex);
Console.WriteLine("Sexy: {0}", blog.Sexy);
Console.WriteLine("Age: {0}", blog.Age);
Console.WriteLine("Ager: {0}", blog.Ager);
Console.WriteLine("Birth: {0}", blog.Birth);
Console.WriteLine("Birthy: {0}", blog.Birthy);
Console.WriteLine("Money: {0}", blog.Money);
Console.WriteLine("Birthy: {0}", blog.Birthy);
Console.WriteLine("Money: {0}", blog.Money);
Console.WriteLine("Moneies: {0}", blog.Moneies);
Console.WriteLine("Pi: {0}", blog.Pi);
Console.WriteLine("Pis: {0}", blog.Pis);
Console.WriteLine("State: {0}", blog.State);
Console.WriteLine("States: {0}", blog.States);
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
TestKdbndp_efcode();
Console.ReadKey();
}
}
}
4.2.3. 参考用例(表逆向为类)
dotnet ef dbcontext scaffold “Host=192.168.29.102;Database=TEST2;Username=SYSTEM;Password=123456;Port=54321” Kdbndp.EntityFrameworkCore.KingbaseES -o Entity