繁体   English   中英

context.ToListAsync() 没有从表中获取任何数据

[英]context.ToListAsync() is not getting any data from the table

该表是在我为“AssetResource”创建 DBSet 之前创建的。 由于我想使用 EF 使其更易于插入或更新,因此我创建了DbSet<AssetResource>而没有调用添加迁移。 该表中已经有一些数据,但是当我调用它时

var list = await context.AssetSources.ToListAsync();

我得到Data is Null然后转到异常。

我尝试添加迁移并成功创建它,但是当我命令update-database时,它告诉我 Resources 表已经存在,这实际上是正确的,因为这个表是在我创建DbSet<AssetResource>之前创建的。

这是获取列表的完整代码:

public async Task<IEnumerable<AssetSource>> GetAssetResources()
        {
            var lAssetSource = new List<AssetSource>();
            try
            {
                var sw = new Stopwatch();
                if (cache.TryGetValue(CACHE_KEYASSETSOURCE, out List<AssetSource> refListAssetSource))
                {
                    helper.LogMessage("Assets Sources are found in cache", LoggerModel.LoggingType.Information);
                    lAssetSource = refListAssetSource;
                }
                else
                {
                    var l = await context.AssetSources.ToListAsync();
                }
                return lAssetSource;
            }
            catch (Exception e)
            {
                helper.LogMessage(e.Message, Shared.Models.LoggerModel.LoggingType.Error);
                throw new NoObjectDetectedException(e.Message);
            }
        }

Data is null表示实体框架尝试将可为空的 db 列绑定到不可为空的属性。 确保如果 db 列是例如int, null实体 model 在 c# 是int? 而不是int

手动删除表,然后运行update-database命令,应该就可以了。 如果删除表,则在运行update-database命令时表中将没有数据。 一个好主意是在运行命令之前重命名现有表,这样您仍然可以在重命名的表中保留现有数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM