DotNetNext / SqlSugar

.Net aot ORM Fastest ORM Simple Easy VB.NET Sqlite orm Oracle ORM Mysql Orm 虚谷数据库 postgresql ORm SqlServer oRm 达梦 ORM 人大金仓 ORM 神通ORM C# ORM , C# ORM .NET ORM NET5 ORM .NET6 ORM ClickHouse orm QuestDb ,TDengine ORM,OceanBase orm,GaussDB orm ,Tidb orm Object/Relational Mapping
https://www.donet5.com/Home/Doc
MIT License
5.35k stars 1.34k forks source link

p.DeptName.SelectAll()获取日期出错 #1112

Closed Tinyipfaan closed 1 year ago

Tinyipfaan commented 1 year ago

实体类:

namespace SqlSugarDemo
{
    /// <summary>
    /// 用户表
    /// </summary>
    [SugarTable("User")]
    public class User
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int UserId { get; set; }
        public string? Name { get; set; }
        public int Age { get; set; }
        public double Weight { get; set; }
        public DateTime CreatedDate { get; set; }
        public int DeptId { get; set; }
        public string DeptName { get; set; }
    }
}

namespace SqlSugarDemo
{
    [SugarTable("Department")]
    public class Department
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int DeptId { get; set; }
        public string? DeptName { get; set; }
        public double Number { get; set; }
    }
}
namespace SqlSugarDemo
{
    public class DefaultContext
    {
        public SqlSugarClient Client { get; }

        public DefaultContext(string connectionString, DbType dbType)
        {
            Client = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = connectionString,//"Data Source=./demo.db",
                DbType = dbType,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
            });
            Client.CodeFirst.InitTables<User>();
            Client.CodeFirst.InitTables<Department>();
        }
    }
}

测试

public class Program
    {
        static void Main(string[] args)
        {
            var context = new DefaultContext("Data Source=./demo.db", DbType.Sqlite);
            //普通查询
            var dtos = context.Client.Queryable<User>()
                .Select(it => new UserDto
                {
                    UserId = it.UserId.SelectAll(),
                }).ToList();
            Console.WriteLine("查询成功");
            //左连接查询
            var depDto = context.Client.Queryable<User, Department>((it, p)
                => new JoinQueryInfos(
                    JoinType.Left, it.DeptId == p.DeptId
                ))
                .Select((it, p)
                => new UserDto
                {
                    DeptName = p.DeptName.SelectAll(),
                }).ToList();
            Console.WriteLine("查询成功");
        }    
    }

第一个查询数据库中日期字段默认值1900-01-01 00:00:00.000 54d7e126e887944bdf929cb61f1221f

第二个查询join之后映射成了0001... b9f109cfd2bd5a8a7920b2af2721214

DotNetNext commented 1 year ago

收到

DotNetNext commented 1 year ago

你这个就该是Left Join 右边表为空

DotNetNext commented 1 year ago

所以都是默认值了

DotNetNext commented 1 year ago

image 你LEFT JOIN右边是空所变成这样了 没有问题

DotNetNext commented 1 year ago

CreatedDate可以设置 DateTime? 这样就返回 null

Tinyipfaan commented 1 year ago

LEFT JOIN即使右边为空,也应该有数据。更何况Department表不为空,查询到是有数据的。是没取到数据,这应该是Bug吧。

DotNetNext commented 1 year ago

你这个代码写错了 , 这样才是对的

UserId =it.UserId.SelectAll() DeptName = p.DeptName,

DotNetNext commented 1 year ago

还有疑问吗?

DotNetNext commented 1 year ago

长时间未回复,先关闭,还有疑问发布新的issue