cockroachdb ef core ssl test

Former-commit-id: 56463bb5083cb21434a9df7b31a1c3b561f4934c
TangShanKaiPing
wanggang 5 years ago
parent 55a86be560
commit 11e7b2ecdf

@ -0,0 +1,19 @@
*.bak
*.suo
*.db
*.db-shm
*.db-wal
*.user
.vs
obj
Obj
bin
Bin
debug
Debug
release
Release
Logs
logs
node_modules
cockroach-data

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
</ItemGroup>
<ItemGroup>
<None Update="client.root.pk12">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30011.22
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CockRoachDBEntityFrameworkCoreTest", "CockRoachDBEntityFrameworkCoreTest.csproj", "{B0398F26-9A0E-4893-9876-5FDAB34C35FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B0398F26-9A0E-4893-9876-5FDAB34C35FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0398F26-9A0E-4893-9876-5FDAB34C35FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0398F26-9A0E-4893-9876-5FDAB34C35FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0398F26-9A0E-4893-9876-5FDAB34C35FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3CB604C6-1B7A-4908-AA58-DAD9A7ED6084}
EndGlobalSection
EndGlobal

@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography.X509Certificates;
namespace CockRoachDBEntityFrameworkCoreTest
{
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//数据库名只能为小写,否则报错为:database "postgres" does not exist
optionsBuilder.UseNpgsql("User ID=root;Host=localhost;Port=26257;Database=mytest;CommandTimeout=120;SslMode=Require;TrustServerCertificate=true;",
o => o.ProvideClientCertificatesCallback(o => o.Add(new X509Certificate2("client.root.pk12"))));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>();
}
}
}

@ -0,0 +1,19 @@
using System;
using System.Linq;
namespace CockRoachDBEntityFrameworkCoreTest
{
public class Program
{
private static void Main(string[] args)
{
using var db = new MyDbContext();
if (db.Database.EnsureCreated())
{
db.Set<User>().Add(new User { Name = "admin" });
db.SaveChanges();
}
Console.WriteLine(db.Set<User>().Count());
}
}
}

@ -0,0 +1,10 @@
文档地址http://doc.cockroachchina.baidu.com/
生成安全证书&启动节点http://doc.cockroachchina.baidu.com/#quick-start/start-a-local-cluster/from-binary/
生成pk12文件openssl pkcs12 -export -password pass: -in client.root.crt -inkey client.root.key -out client.root.pk12
命令行创建数据库:
cockroach sql --certs-dir certs
>create database MyTest;
>\q

@ -0,0 +1,15 @@
using System;
namespace CockRoachDBEntityFrameworkCoreTest
{
public class User
{
public User()
{
this.Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public string Name { get; set; }
}
}

Binary file not shown.
Loading…
Cancel
Save