mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 12:09:29 +08:00
Update Mongodb
This commit is contained in:
@@ -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;
|
||||
|
49
Src/Asp.NetCore2/MongoDbTest/DBHelper/DbHelper.cs
Normal file
49
Src/Asp.NetCore2/MongoDbTest/DBHelper/DbHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -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";
|
||||
}
|
||||
}
|
24
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/Order.cs
Normal file
24
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/Order.cs
Normal 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; }
|
||||
}
|
||||
}
|
18
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/OrderItem.cs
Normal file
18
Src/Asp.NetCore2/MongoDbTest/OrmTest/Models/OrderItem.cs
Normal 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; }
|
||||
}
|
||||
}
|
20
Src/Asp.NetCore2/MongoDbTest/OrmTest/OrmTest.cs
Normal file
20
Src/Asp.NetCore2/MongoDbTest/OrmTest/OrmTest.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
using MongoDbTest;
|
||||
//开发中
|
||||
OrmTest.Init();
|
||||
AdoTest.Init();
|
||||
ExpTest.Init();
|
||||
Console.WriteLine("执行完成");
|
||||
|
Reference in New Issue
Block a user