Newcats.DataAccess.PostgreSql 1.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Newcats.DataAccess.PostgreSql --version 1.2.1                
NuGet\Install-Package Newcats.DataAccess.PostgreSql -Version 1.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Newcats.DataAccess.PostgreSql" Version="1.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Newcats.DataAccess.PostgreSql --version 1.2.1                
#r "nuget: Newcats.DataAccess.PostgreSql, 1.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Newcats.DataAccess.PostgreSql as a Cake Addin
#addin nuget:?package=Newcats.DataAccess.PostgreSql&version=1.2.1

// Install Newcats.DataAccess.PostgreSql as a Cake Tool
#tool nuget:?package=Newcats.DataAccess.PostgreSql&version=1.2.1                

Newcats.DataAccess.PostgreSql 使用说明

Net Core Nuget GitHub License

1.实体类

  • 1.数据库实体类以Entity结尾
  • 2.使用相关特性,对实体类属性进行设置
  • TableAttribute:数据库表名,多表连接时为对应的连接关系
  • KeyAttribute:数据库主键标识
  • DatabaseGeneratedAttribute:数据库生成特性,标识自增、计算列等(插入时会忽略此字段)
  • NotMappedAttribute:数据库中不存在此字段时,使用此特性忽略该字段
  • ColumnAttribute:实体类别名映射特性,标注数据库实际字段名

默认约定

  • 1.若不使用特性,则程序按默认约定进行解析
  • 2.表名称为类名,或者类名去掉Entity字符串
  • 3.主键为Id字段,或者Id结尾的字段
  • 4.推荐使用特性进行设置
UserEntity.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("User")]
public class UserEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }

    public string AddressId { get; set; }

    [NotMapped]
    public string Phone { get; set; }
}

[Table("Address")]
public class AddressEntity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Name { get; set; }
}

[Table(" User a left join Address b on a.AddressId=b.Id ")]
public class UserDto
{
    [Column("a.Id")]
    public int Id { get; set; }

    [Column("a.Name")]
    public string Name { get; set; }

    [Column("b.Name")]
    public string Address { get; set; }
}

2.新建自定义DbContext,继承自 Newcats.DataAccess.PostgreSql.DbContext

  • 注1:不同的数据库类型继承不同的DbContext基类,此处以PostgreSql为例
  • 注2:若使用了多个数据库连接字符串,需要为每个连接字符串分别定义DbContext
MyDbContext.cs
public class MyDbContext : DbContext
{
    public MyDbContext(IOptions<DataAccess.Core.DbContextOptions> optionsAccessor) : base(optionsAccessor)
    {
    }
}
OtherDbContext.cs
public class OtherDbContext : DbContext
{
    public OtherDbContext(IOptions<DataAccess.Core.DbContextOptions> optionsAccessor) : base(optionsAccessor)
    {
    }
}

3.Startup.cs类里注册相应的服务

Startup.cs
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        //第一个DbContext
        services.AddPostgreSqlDataAccess<MyDbContext>(opt =>
        {
            opt.ConnectionString = "ConnectionStringOfMyDbContext";
        });

        //第二个DbContext
        //同一个应用可以注册不同的数据库,例如:services.AddSqlServerDataAccess...
        services.AddPostgreSqlDataAccess<OtherDbContext>(opt =>
        {
            opt.ConnectionString = "ConnectionStringOfOtherDbContext";
        });
    }
}

4.服务层使用依赖注入获取

UserService.cs
public class UserService : IUserService
{
    private readonly Newcats.DataAccess.PostgreSql.IRepository<MyDbContext> _myRepository;
    private readonly Newcats.DataAccess.PostgreSql.IRepository<OtherDbContext> _otherRepository;

    public UserService(Newcats.DataAccess.PostgreSql.IRepository<MyDbContext> myRepository, Newcats.DataAccess.PostgreSql.IRepository<OtherDbContext> otherRepository)
    {
         _myRepository = myRepository;
        _otherRepository = otherRepository;
    }

    public async Task<UserEntity> GetAsync(int id)
    {
        //根据主键Id获取一条记录
        return await _myRepository.GetAsync<UserEntity>(id);
    }

    public async Task<IEnumerable<AddressInfo>> GetListAsync(int top)
    {
        //获取Id>=30的前top条记录
        return await _otherRepository.GetTopAsync<AddressInfo>(top, new List<DbWhere>()
        {
            new DbWhere(t=>t.Id, 30, OperateTypeEnum.GreaterEqual)
        });
    }
}

贡献与反馈

如果你在阅读或使用任意一个代码片断时发现Bug,或有更佳实现方式,欢迎提Issue。

对于你提交的代码,如果我们决定采纳,可能会进行相应重构,以统一代码风格。

对于热心的同学,将会把你的名字放到贡献者名单中。


免责声明

  • 虽然代码已经进行了高度审查,并用于自己的项目中,但依然可能存在某些未知的BUG,如果你的生产系统蒙受损失,本人不会对此负责。
  • 出于成本的考虑,将不会对已发布的API保持兼容,每当更新代码时,请注意该问题。

协议

MIT © Newcats


作者: newcats-2021/12/05

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 515 1/22/2022
1.2.6 287 1/9/2022
1.2.5 286 12/28/2021
1.2.3 275 12/26/2021
1.2.2 281 12/26/2021
1.2.1 257 12/23/2021
1.2.0 301 12/19/2021
1.0.1 287 12/15/2021
1.0.0 313 12/9/2021

2021-12-23
1.升级Npgsql包

2021-12-12
1.增加TableAttribute.Schema支持

2021-12-16 v1.0.1
1.完成SqlBulkCopy实现

2021-12-09 v1.0.0
1.完成基本的CRUD方法(SqlBulkCopy暂未实现)
2.增加详细的使用说明文档,详见github