我用的.net 6
- 安装依赖包
> Microsoft.EntityFrameworkCore 6.0.33 6.0.33
> Microsoft.EntityFrameworkCore.Tools 6.0.33 6.0.33
> Npgsql.EntityFrameworkCore.PostgreSQL 6.0.29 6.0.29
> Z.EntityFramework.Extensions.EFCore 6.103.4 6.103.4
- 添加配置
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"DataBaseConfig": {
"Host": "192.168.214.133",
"Port": 32222,
"UserName": "postgresadmin",
"Password": "admin123",
"DataBase": "postgresdb"
}
}
- 添加实体类,实体配置,以及配置类
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace EFCoreBulkInsert
{
public class Config
{
public int Port {
get; set; }
public string Host {
get; set; }
public string UserName {
get; set; }
public string Password {
get; set; }
public string DataBase {
get; set; }
}
public class Test
{
public int ID {
get; set; }
public string Name {
get; set; }
}
public class TestConfig : IEntityTypeConfiguration<Test>
{
public void Configure(EntityTypeBuilder<Test> builder)
{
builder.ToTable("test");
builder.HasKey(t => t.ID);
builder.Property(t