【问题标题】:entity framework core orderby guid failing to order properly实体框架核心 orderby guid 无法正确排序
【发布时间】:2017-11-01 18:25:46
【问题描述】:

我正在尝试通过 EF Core 中的 Guid 与关系数据库进行排序,但它没有排序。是不是我做错了什么,或者这可能是 EF Core 的问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;

namespace TestName
{
    public class BoxDbContext : DbContext
    {
        public BoxDbContext(
            DbContextOptions<BoxDbContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Box>().HasKey(x => x.Id);
            modelBuilder.Entity<Box>().Property(t => t.Id).ValueGeneratedOnAdd();
            base.OnModelCreating(modelBuilder);
        }
    }

    public class Box
    {
        public Guid Id { get; set; }
        public Guid SubId { get; set; }
    }

    [TestFixture]
    public class TestClass
    {
        private SqliteConnection SqliteConnection { get; set; }

        private DbContextOptions<BoxDbContext> Options => new DbContextOptionsBuilder<BoxDbContext>()
            .UseSqlite(SqliteConnection)
            .EnableSensitiveDataLogging()
            .Options;

        private DbContext GetDbContext()
        {
            BoxDbContext dbContext = new BoxDbContext(Options);
            dbContext.Database.EnsureCreated();
            return dbContext;
        }

        [SetUp]
        public void DbSetup()
        {
            SqliteConnectionStringBuilder sqliteConnectionStringBuilder = new SqliteConnectionStringBuilder
            {
                Mode = SqliteOpenMode.Memory,
                Cache = SqliteCacheMode.Private
            };
            SqliteConnection = new SqliteConnection(sqliteConnectionStringBuilder.ToString());
            SqliteConnection.Open();
        }

        [TearDown]
        public void DbTearDown()
        {
            SqliteConnection.Close();
        }

        [Test]
        public async Task OrderByGuid()
        {
            List<Guid> subIds = new List<Guid>
            {
                Guid.Parse("901CAB07-315F-4594-A5C6-C37725643DB8"),
                Guid.Parse("FA1760E7-27F4-4F8B-9205-44ACF2358044"),
                Guid.Parse("0C434803-0004-4894-8E29-597AA8BCF8E2"),
                Guid.Parse("C7E76CF2-35D1-4CF8-8A67-83F41842F052"),
                Guid.Parse("1D6F9038-B5B3-4559-9480-3A2651E52623"),
            };

            using (DbContext dbContext = GetDbContext())
            {
                foreach (Guid subId in subIds)
                {
                    dbContext.Set<Box>().Add(new Box {SubId = subId});
                }
                await dbContext.SaveChangesAsync();
            }

            IList<Box> boxs;
            using (DbContext approvalsDbContext = GetDbContext())
            {
                boxs = await approvalsDbContext
                    .Set<Box>()
                    .OrderByDescending(x => x.SubId)
                    .ToListAsync();
            }

            Assert.That(boxs.Count, Is.EqualTo(subIds.Count));
            Assert.That(boxs.ToArray()[0].SubId, Is.EqualTo(subIds[1]));
            Assert.That(boxs.ToArray()[1].SubId, Is.EqualTo(subIds[3]));
            Assert.That(boxs.ToArray()[2].SubId, Is.EqualTo(subIds[0]));
            Assert.That(boxs.ToArray()[3].SubId, Is.EqualTo(subIds[4]));
            Assert.That(boxs.ToArray()[4].SubId, Is.EqualTo(subIds[2]));
        }
    }
}

谢谢, 克里斯

【问题讨论】:

  • 如果我通过第一个字符引用您的 GUID,您是在告诉我输出不是:0,1,9,C,F ?
  • 我的意思正好相反,刚刚看到是降序
  • 我先拿回 C :(

标签: entity-framework entity-framework-core ef-core-2.0


【解决方案1】:

所以我向 EF Core 团队提出了这个问题,他们说这是 SQLite 的预期行为,因为它没有 Guid 的表示。 https://github.com/aspnet/EntityFrameworkCore/issues/10198#issuecomment-340930189

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2021-12-18
    • 1970-01-01
    • 2021-02-15
    • 2020-10-15
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多