亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對數據庫的查詢返回空或 null 但保存數據確實有效

對數據庫的查詢返回空或 null 但保存數據確實有效

C#
翻閱古今 2022-11-21 15:49:56
我正在使用帶有 Entity Framework Core 的 ASP.net MVC 核心創建一個 Web API。我正在使用 NSwag 來使用 swagger 文檔和 Swagger UI,我正在測試 Post 方法并且它們有效,但 get 返回空或 null。在這里可以看到數據庫中的數據獲取表的所有數據的代碼是這樣的: // GET: api/UserRoles        [HttpGet]        public IEnumerable<UserRole> GetUserRoles()        {            return _context.UserRoles.ToList();        }但即使數據庫中有數據,它也返回 null,并且所有其他查詢返回 null 或空(或在以下情況下拋出異常.First()這是我的OnModelCreatingprotected override void OnModelCreating(ModelBuilder modelBuilder)    {        base.OnModelCreating(modelBuilder);        //Debugger.Launch();        modelBuilder.Entity<Category>().HasOne(x => x.ParentCategory).WithMany(x => x.SubCategories).HasForeignKey(x => x.ParentCategoryId);        foreach (var entity in modelBuilder.Model.GetEntityTypes())        {            var type = entity.ClrType.GetInterface(nameof(Interfaces.IDto));            if (type == null) continue;            modelBuilder.Entity(entity.ClrType).Property<DateTime?>("DeletedAt");            modelBuilder.Entity(entity.ClrType)                .Property<DateTime>("LastUpdated")                .HasComputedColumnSql("SYSUTCDATETIME()");            modelBuilder.Entity(entity.ClrType)                .Property<DateTime>("CreatedAt").HasDefaultValueSql("SYSUTCDATETIME()"); ;            modelBuilder.Entity(entity.ClrType)                .HasKey(nameof(Interfaces.IDto.Id)).ForSqlServerIsClustered(false);            modelBuilder.Entity(entity.ClrType)                .HasIndex("CreatedAt").ForSqlServerIsClustered();            var parameter = Expression.Parameter(entity.ClrType, "e");            var body = Expression.NotEqual(                Expression.Call(typeof(EF), nameof(EF.Property), new[] { typeof(DateTime?) }, parameter, Expression.Constant("DeletedAt")),                Expression.Constant(null));            modelBuilder.Entity(entity.ClrType).HasQueryFilter(Expression.Lambda(body, parameter));        }        modelBuilder.Entity<Customer>().HasIndex(x => x.Identification).IsUnique();    }
查看完整描述

2 回答

?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

問題在Expression.NotEqual這里:


var parameter = Expression.Parameter(entity.ClrType, "e");

var body = Expression.NotEqual(

    Expression.Call(typeof(EF), nameof(EF.Property), new[] { typeof(DateTime?) }, parameter, Expression.Constant("DeletedAt")),

    Expression.Constant(null));

modelBuilder.Entity(entity.ClrType).HasQueryFilter(Expression.Lambda(body, parameter));

當前所做的是設置一個類似于此(偽代碼)的全局查詢過濾器(即應用于所有查詢的附加條件):


e => e.DeletedAt != null

這將返回所有軟刪除記錄(在你的情況下沒有),而我想這個想法是返回非軟刪除記錄,即


e => e.DeletedAt == null

所以只需更改Expression.NotEqual為Expression.Equal,問題就會得到解決。


查看完整回答
反對 回復 2022-11-21
?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

您需要創建一個查詢以從您的上下文中獲取特定信息。

使用上下文創建對表的查詢。

例如。

var query = context.Students
                   .where(s => s.StudentName == "Bill")
                   .FirstOrDefault<Student>();

查詢.tolist()


查看完整回答
反對 回復 2022-11-21
  • 2 回答
  • 0 關注
  • 173 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號