c# - 元素未添加到列表 .NET Core
问题描述
我有一个压力很大的问题,我无法将元素添加到列表中,该操作不会引发任何异常或任何东西,只是没有添加元素。
结构如下。
我有一个班级,我将其用作内存数据库。在该类中,我有一个具有一些默认值的对象列表。
对象类型是 Author 具有以下结构
public class Author
{
public Guid Id { get; set; }
public string Name { get; set; }
}
列表结构是这样的
public class InMemoryStore
{
public List<Author> Authors => new List<Author>
{
new Author(new Guid("5784f8b7-31b5-4886-8874-aff5241164a8"), "test"),
}
}
我正在将该类注册为 Scoped 服务并将其注入我的测试控制器中,只需执行添加,它不会产生任何结果。它不会添加它不会引发任何异常的项目。我很困惑。
private readonly InMemoryStore _inMemoryStore;
public AuthorsController(InMemoryStore inMemoryStore)
{
_inMemoryStore = inMemoryStore;
}
在行动
_inMemoryStore.Authors.Add(author);
在动作中创建相同的列表,有效
解决方案
我想你的意思是:
public List<Author> Authors { get; } = new List<Author>
{
new Author(new Guid("5784f8b7-31b5-4886-8874-aff5241164a8"), "test"),
}
推荐阅读
- python-3.x - 如何使用 AWS Rekognition、AWS Lambda 和 Python 3.8 设计内容审核服务
- angular - Angular 和 Laravel 高级搜索
- android - 从启动器图标启动应用程序时如何获取通知数据
- r - 安装 R 包失败
- react-native - 尝试使用 React Native 和 Expo 在 c# 中使用 API REST 时的值“未定义”
- c++ - 如何通过 C++ 向量更新 mongo 数组
- authentication - Nuxt Auth 在通过模式登录完成后给出错误
- java - 在android studio中使用java创建蓝牙遥控器的问题
- javascript - url 行的正则表达式
- java - 如果 Spring Boot 失败,如何停止重新尝试与 RabbitMq、Postgress 建立连接?