[英]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.