两行代码即可,实现对数据库的操作

1.配置数据库连接

1.1配置

{
  //数据库连接
  "ConnectionStrings": {
    "DefaultConnection": "Server=127.0.0.1;Uid=root;Pwd=123456;Database=test;Port=3306;",
    "DefaultConnection2": "Server=127.0.0.1;Uid=root;Pwd=123456;Database=rswl;Port=3306;"
  } 
}

1.2Startup.cs文件中

public IServiceProvider ConfigureServices(IServiceCollection services)
  {
     string connStr =Configuration.GetConnectionString("DefaultConnection"); 
     services.AddDatabase<MySqlDatabaseProvider>(connStr); 
     services.AddUnitOfWork();
  }

2.操作数据库

例: UserRepository.cs


using System; 
using PetaPoco;    
public class UserRepository
{  
     IDatabase db;
     public UserRepository(IApplicationDbContext _db)
    {
       db=_db.Current;
    }

     public void Get()
     {
            //获取数据库连接,注意这里使用的是appsetting.json文件中数据库连接字符串的key,不是其值
            var db1 = GetDatabase("DefaultConnection");
            var db2 = GetDatabase("DefaultConnection2");

            //访问数据库
            var u1=db1.FirstOrDefault<UserInfo>("");
            var u2 = db2.FirstOrDefault<UserInfo>(""); 
      }
}

可从Nuget下载,所使用的数据库驱动

PetaPoco支持的数据库有

数据库驱动类名称 支持的数据库驱动 Other
SqlServerDatabaseProvider SqlServer, System.Data.SqlClient Is the default
MySqlDatabaseProvider MySql,MySql.Data.MySqlClient
MariaDbDatabaseProvider MariaDb May use MySql provider
PostgreSQLDatabaseProvider Npgsql, pgsql
SQLiteDatabaseProvider SQLite, System.Data.SQLite
SqlServerCEDatabaseProviders SqlServerCe, SqlCeConnection, System.Data.SqlServerCe.4.0
OracleDatabaseProvider Oracle, Oracle.ManagedDataAccess.Client
MsAccessDbDatabaseProvider OLEDB Connection string must have either Microsoft.ACE.OLEDB.12.0 or Microsoft.Jet.OLEDB.4.0
FirebirdDbDatabaseProvider Firebird, FbConnection beta

用到哪个就去Nuget上搜

RsCode使用PetaPoco做为ORM,官方文档

Petapoco默认使用的数据库是mssql,文档

实例代码:

//获取sqlserver数据库连接
string connStr =Configuration.GetConnectionString("DefaultConnection"); 
services.AddDatabase<MySqlDatabaseProvider>(connStr);

//获取数据库连接
var db = GetDatabase("DefaultConnection"); 

//插入10000万数据
 for (int i = 0; i < 10000; i++)
    {
        using (db)
        {
            await db.InsertAsync(new RsCode.TestModel.UserInfo { UserName = DateTime.Now.ToString("yyyyMMddHHmmssfff"), Age = 10 }); 
        }

   }

//插入操作https://github.com/CollaboratingPlatypus/PetaPoco/wiki/Inserting

//事务操作https://github.com/CollaboratingPlatypus/PetaPoco/wiki/Transactions

//存储过程https://github.com/CollaboratingPlatypus/PetaPoco/wiki/Stored-procedures

//1对多,多对1 https://github.com/CollaboratingPlatypus/PetaPoco/wiki/Multi-Poco-Queries

注意事项:

Petapoco 类模型的定义

    public class RoleInfo
    {      
        public int Id{ get; set; }     
        public string RoleName { get; set; }      
        public string Remark { get; set; } = "";
    }

默认类名就是表名,默认主键是Id ,默认自增长

如果更换了默认值,就写全它

   /// <summary>
    /// 角色信息
    /// </summary>
    [TableName("rswl_role_info")]
    [PrimaryKey("id")]
    public class RoleInfo
    {
        [Column("id")]
        public int RoleId { get; set; }
       /// <summary>
       /// 角色名称
       /// </summary>
        public string RoleName { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; } = "";
    }

results matching ""

    No results matching ""