简体   繁体   English

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

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

The table was created before I created the DBSet for "AssetResource".该表是在我为“AssetResource”创建 DBSet 之前创建的。 Since I want to use the EF to make it easier to insert or update, I created the DbSet<AssetResource> without calling the add-migration.由于我想使用 EF 使其更易于插入或更新,因此我创建了DbSet<AssetResource>而没有调用添加迁移。 The table has some data in it already, but when I call using this该表中已经有一些数据,但是当我调用它时

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

I get an error of Data is Null then goes to the Exception.我得到Data is Null然后转到异常。

I tried adding the migration and creates it successfully but when I command the update-database , it tells me that the Resources table is already existing which is actually right because this table has been created before I created the DbSet<AssetResource> .我尝试添加迁移并成功创建它,但是当我命令update-database时,它告诉我 Resources 表已经存在,这实际上是正确的,因为这个表是在我创建DbSet<AssetResource>之前创建的。

Here's the whole code of getting the list:这是获取列表的完整代码:

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 means that entity framework tries to bind nullable db column to non nullable property. Data is null表示实体框架尝试将可为空的 db 列绑定到不可为空的属性。 Make sure that if the db column is for example int, null that the entity model in c# is int?确保如果 db 列是例如int, null实体 model 在 c# 是int? and not int .而不是int

Manually delete the table and then run the update-database command and you should be good.手动删除表,然后运行update-database命令,应该就可以了。 If you delete the table there will be no data in the table when running the update-database command.如果删除表,则在运行update-database命令时表中将没有数据。 A good idea would be to rename the existing table before running the command, so you still have the existing data in the renamed table.一个好主意是在运行命令之前重命名现有表,这样您仍然可以在重命名的表中保留现有数据。

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

相关问题 Azure MobileService客户端-ToListAsync不会检索所有数据 - Azure MobileService Client - ToListAsync not retrieving all data 从ASPNetBoilerplate中的SqlQuery从自定义存储库访问ToListAsync() - Accessing ToListAsync() from Custom Repository for SqlQuery in ASPNetBoilerplate .NET 6:context.db.Where().Orderby().ToListAsync() 不起作用 - .NET 6 : context.db.Where().Orderby().ToListAsync() does not work 从网页表获取数据 - Getting data from a webpage table ApplicationDbContext ToListAsync() 不返回“Id”列数据 - ApplicationDbContext ToListAsync() not returning 'Id' Column data 通过主键从外表获取数据 - Getting Data from foreign table by primary key 使用Selenium从表中获取数据 - Getting data from table using Selenium 从表中读取数据时出现错误 - Getting Error while read the data from table 方法获取列表异步? &#39;清单 <ListDefinition> &#39;不包含&#39;ToListAsync&#39;的定义 - Method for getting a list async? 'List<ListDefinition>' does not contain a definition for 'ToListAsync' 从其他程序集中获取上下文 - Getting the context from a different assembly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM