Update Mongodb

This commit is contained in:
sunkaixuan
2025-05-04 15:59:46 +08:00
parent 739961e618
commit c377d1e0ae
13 changed files with 205 additions and 84 deletions

View File

@@ -1,6 +1,7 @@
using MongoDb.Ado.data;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbTest.DBHelper;
using System;
using System.Collections.Generic;
using System.Data;

View File

@@ -0,0 +1,49 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest.DBHelper
{
/// <summary>
/// Helper class for database operations
/// 数据库操作的辅助类
/// </summary>
public class DbHelper
{
public static string ConnectionString = "mongodb://mongouser:Huangxin%40123@lb-pvnmcqnd-81l6ojfdw3u4en92.clb.sh-tencentclb.com:27018/SqlSugarDb?replicaSet=cmgo-7d07e4w1_0&authSource=admin";
public static string SqlSugarConnectionString = "host=lb-pvnmcqnd-81l6ojfdw3u4en92.clb.sh-tencentclb.com;Port=27018;Database=SqlSugarDb;Username=mongouser;Password=Huangxin@123;replicaSet=cmgo-7d07e4w1_0&authSource=admin";
/// <summary>
/// Get a new SqlSugarClient instance with specific configurations
/// 获取具有特定配置的新 SqlSugarClient 实例
/// </summary>
/// <returns>SqlSugarClient instance</returns>
public static SqlSugarClient GetNewDb()
{
//注册DLL防止找不到DLL
InstanceFactory.CustomAssemblies = new System.Reflection.Assembly[] {
typeof(SqlSugar.MongoDb.MongoDbProvider).Assembly };
//创建DB
var db = new SqlSugarClient(new ConnectionConfig()
{
IsAutoCloseConnection = true,
DbType = DbType.MongoDb,
ConnectionString = SqlSugarConnectionString,
LanguageType = LanguageType.Default//Set language
},
it =>
{
it.Aop.OnLogExecuting = (sql, para) =>
{
Console.WriteLine(UtilMethods.GetNativeSql(sql, para));
};
});
return db;
}
}
}

View File

@@ -1,15 +0,0 @@
using MongoDB.Driver.Core.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
public class DbHelper
{
public static string ConnectionString= "mongodb://mongouser:Huangxin%40123@localhost:27018/SqlSugarDb?replicaSet=cmgo-7d07e4w1_0&authSource=admin";
public static string SqlSugarConnectionString = "host=localhost;Port=27018;Database=SqlSugarDb;Username=mongouser;Password=Huangxin@123;replicaSet=cmgo-7d07e4w1_0&authSource=admin";
}
}

View File

@@ -0,0 +1,24 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MongoDbTest
{
public class OrderInfo
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true,ColumnName ="_Id")]
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
[SugarColumn(IsNullable = true)]
public DateTime CreateTime { get; set; }
[SugarColumn(IsNullable =true)]
public int CustomId { get; set; }
[SugarColumn(IsIgnore = true)]
public List<OrderItem> Items { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MongoDbTest
{
[SqlSugar.SugarTable("OrderDetail")]
public class OrderItem
{
[SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity =true, ColumnName = "_Id")]
public int Id { get; set; }
public int OrderId { get; set; }
public decimal? Price { get; set; }
[SqlSugar.SugarColumn(IsNullable = true)]
public DateTime? CreateTime { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
using MongoDbTest.DBHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
public class OrmTest
{
public static void Init()
{
var db = DbHelper.GetNewDb();
db.Insertable(new OrderInfo() { CreateTime = DateTime.Now, Name = "a", Price = 1 })
.ExecuteCommand();
}
}
}

View File

@@ -1,5 +1,6 @@
using MongoDbTest;
//开发中
OrmTest.Init();
AdoTest.Init();
ExpTest.Init();
Console.WriteLine("执行完成");