mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-07 07:04:32 +08:00
Compare commits
2 Commits
61a35f2a56
...
7832ab747d
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7832ab747d | ||
![]() |
a92d5ef77a |
@@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
@@ -39,6 +39,7 @@ namespace MongoDbTest
|
||||
Enum.Init();
|
||||
Enum2.Init();
|
||||
UnitDateTimeFunc.Init();
|
||||
UnitDateOnly.Init();
|
||||
//主键不是ObjectId类型用例
|
||||
//The primary key is not an ObjectId type use case
|
||||
LongPrimaryKey.Init();
|
||||
|
42
Src/Asp.NetCore2/MongoDbTest/UnitTest/UnitDateOnly.cs
Normal file
42
Src/Asp.NetCore2/MongoDbTest/UnitTest/UnitDateOnly.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using MongoDB.Bson;
|
||||
using SqlSugar.MongoDb;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbTest
|
||||
{
|
||||
internal class UnitDateOnly
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
var db = DbHelper.GetNewDb();
|
||||
db.DbMaintenance.TruncateTable<DateOnlyModel>();
|
||||
var dt = DateOnly.FromDateTime(Convert.ToDateTime("2022-01-01"));
|
||||
var dt2 = DateOnly.FromDateTime(Convert.ToDateTime("2022-11-01"));
|
||||
db.Insertable(new DateOnlyModel()
|
||||
{
|
||||
DateOnly=dt
|
||||
|
||||
}).ExecuteCommand();
|
||||
db.Insertable(new DateOnlyModel()
|
||||
{
|
||||
DateOnly = DateOnly.FromDateTime(Convert.ToDateTime("2022-12-01"))
|
||||
|
||||
}).ExecuteCommand();
|
||||
var list = db.Queryable<DateOnlyModel>().ToList();
|
||||
|
||||
var list2 = db.Queryable<DateOnlyModel>().Where(it=>it.DateOnly==dt).ToList();
|
||||
var list3 = db.Queryable<DateOnlyModel>().Where(it => it.DateOnly == dt2).ToList();
|
||||
if (list2.Count != 1 || list3.Count !=0 || list.Count != 2) Cases.ThrowUnitError();
|
||||
}
|
||||
}
|
||||
|
||||
public class DateOnlyModel : MongoDbBase
|
||||
{
|
||||
public DateOnly DateOnly { get; set; }
|
||||
}
|
||||
}
|
@@ -147,6 +147,12 @@ namespace SqlSugar.MongoDb
|
||||
return arrayObj;
|
||||
}
|
||||
}
|
||||
internal static object DateOnlyToDateTime(object value)
|
||||
{
|
||||
if (value == null) return null;
|
||||
var method = value.GetType().GetMethods().First(it => it.GetParameters().Length == 0 && it.Name == "ToShortDateString");
|
||||
return method.Invoke(value, new object[] { });
|
||||
}
|
||||
public static BsonValue MyCreate(object value)
|
||||
{
|
||||
if (value is DateTime dt)
|
||||
@@ -162,6 +168,10 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
value = ObjectId.Parse(s);
|
||||
}
|
||||
else if (value!=null&&value?.GetType()?.Name=="DateOnly")
|
||||
{
|
||||
value = Convert.ToDateTime(DateOnlyToDateTime(value)).ToString("yyyy-MM-dd");
|
||||
}
|
||||
return BsonValue.Create(value);
|
||||
}
|
||||
internal static MongoDB.Bson.IO.JsonWriterSettings GetJsonWriterSettings()
|
||||
|
Reference in New Issue
Block a user