gpt4 book ai didi

c# - 对数据库的查询返回空或 null 但保存数据确实有效

转载 作者:行者123 更新时间:2023-12-05 03:01:52 25 4
gpt4 key购买 nike

我正在使用 ASP.net MVC Core 和 Entity Framework Core 创建 Web API。我正在使用 NSwag 来使用 swagger 文档和 Swagger UI,我正在测试 Post 方法并且它们有效,但 get 返回空或 null。

这里可以看到数据库中的数据

enter image description here

获取一个表的所有数据的代码是这样的:

 // GET: api/UserRoles
[HttpGet]
public IEnumerable<UserRole> GetUserRoles()
{
return _context.UserRoles.ToList();
}

但即使数据库中有数据,它也返回 null,而所有其他查询都返回 null 或空(或者在 .First()

的情况下抛出异常

这是我的OnModelCreating

protected 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();
}

这是我的 UserRole 模型

public class UserRole:Dbo
{
public string Name { set; get; }
public string Description { get; set; }
}

这是我的Dbo

public abstract class Dto : IDto, INotifyPropertyChanged
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

所有模型的所有查询都返回 null 或空

更新:

在这里你可以看到一个Where()方法被调用了0个结果(我只有一个name等于“admin”的记录),你也可以看到查询正在查询的 StockMovementFlow 表并返回 0 条记录。

enter image description here

最佳答案

问题是 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));

目前所做的是设置一个global query filter (即应用于所有查询的附加条件)类似于此(伪代码):

e => e.DeletedAt != null

这将返回所有软删除记录(在您的情况下没有),而我想这个想法是返回非软删除记录,即

e => e.DeletedAt == null

所以只需将 Expression.NotEqual 更改为 Expression.Equal 即可解决问题。

关于c# - 对数据库的查询返回空或 null 但保存数据确实有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55436618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com