C# 注入的AppDbContex但并非所有数据都会在后续调用中被记住

C# 注入的AppDbContex但并非所有数据都会在后续调用中被记住,c#,dependency-injection,asp.net-core-mvc,C#,Dependency Injection,Asp.net Core Mvc,我希望我能解释清楚。。。我有一个.NET核心应用程序,我在启动的ConfigureServices方法中注册AppDbContext,如下所示: services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase()); services.AddDbContext(options=>options.UseInMemoryDatabase()); 我正在使用CommandHandler保存资产。

我希望我能解释清楚。。。我有一个.NET核心应用程序,我在启动的ConfigureServices方法中注册AppDbContext,如下所示:

services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase());
services.AddDbContext(options=>options.UseInMemoryDatabase());
我正在使用CommandHandler保存资产。命令处理程序代码如下所示:

public class SaveAssetCommandHandler : ICommandHandler<SaveAssetCommandHandler.SaveAssetCommand>
{
    private readonly AppDbContext _context;

    public SaveAssetCommandHandler(AppDbContext context)
    {
        _context = context;
    }

    public void Handle(SaveAssetCommand command)
    {
        var asset = command.Id != null
            ? _context.Set<Asset>()
                .Single(o => o.Id == command.Id)
            : new Asset();

        var assetType = _context.Set<AssetType>().SingleOrDefault(t => t.Id == command.AssetTypeId);

        asset.Id = command.Id;
        asset.SerialNumber = command.SerialNumber;
        asset.PartNumber = command.PartNumber;
        asset.AssetType = assetType;

        if (command.Id == null)
            _context.Set<Asset>().Add(asset);
    }

    public class SaveAssetCommand
    {
        public string Id { get; set; }
        public string SerialNumber { get; set; }
        public string PartNumber { get; set; }
        public int AssetTypeId { get; set; }
        public AssetType AssetType { get; set; }
    }
}
公共类SaveAssetCommandHandler:ICommandHandler
{
私有只读AppDbContext\u上下文;
公共SaveAssetCommandHandler(AppDbContext上下文)
{
_上下文=上下文;
}
公共无效句柄(SaveAssetCommand命令)
{
var asset=command.Id!=null
?_context.Set()
.Single(o=>o.Id==command.Id)
:新资产();
var assetType=\u context.Set().SingleOrDefault(t=>t.Id==command.AssetTypeId);
asset.Id=command.Id;
asset.SerialNumber=command.SerialNumber;
asset.PartNumber=command.PartNumber;
asset.AssetType=资产类型;
if(command.Id==null)
_context.Set().Add(资产);
}
公共类SaveAssetCommand
{
公共字符串Id{get;set;}
公共字符串序列号{get;set;}
公共字符串零件号{get;set;}
公共int资产类型ID{get;set;}
公共资产类型资产类型{get;set;}
}
}
现在,这里将注入AppDbContext,并设置模型的属性(注意,我用一个值设置了asset.AssetType属性)。控件返回到调用程序,AppDbContext也在其中被注入,并调用SaveChanges

当我返回控制器时,我调用RedirectToAction到Index操作,在这里我使用AssetPository列出我的所有资产:

public class AssetRepository : IRepository<Asset>
{
    private static ConcurrentDictionary<string, Asset> _Assets = new ConcurrentDictionary<string, Asset>();
    private readonly AppDbContext _dbContext;

    public AssetRepository(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public Task<Asset> GetByIdAsync(string id)
    {
        return _dbContext.Assets
            .FirstOrDefaultAsync(s => s.Id == id);
    }

    public IEnumerable<Asset> GetAll()
    {
        return _Assets.Values;
    }

    public Task<List<Asset>> ListAsync()
    {
        return _dbContext.Assets
            .OrderByDescending(s => s.ManufacturedDate)
            .ToListAsync();
    }
公共类资产存储:i存储
{
私有静态ConcurrentDictionary_Assets=新ConcurrentDictionary();
私有只读AppDbContextu dbContext;
公共资产存储(AppDbContext)
{
_dbContext=dbContext;
}
公共任务GetByIdAsync(字符串id)
{
return\u dbContext.Assets
.FirstOrDefaultAsync(s=>s.Id==Id);
}
公共IEnumerable GetAll()
{
返回资产价值;
}
公共任务ListAsync()
{
return\u dbContext.Assets
.OrderByDescending(s=>s.ManufacturedDate)
.ToListAsync();
}
请再次注意,我再次注入了AppDbContext。但是,当调用ListAsync时,将返回我的资产列表,但其中一个资产上的AssetType属性为null。在调用SaveChanges时,它不是null


所以我的问题是:当AppDbContext在这些后续调用中被注入时,它是否还记得保存的对象所包含的内容?如果是,那么为什么我在列出时会丢失AssetType属性。

读这篇文章:感谢您的有趣阅读。我了解到,我的依赖项都在每个请求的范围内,这意味着我的AppDbContext和我的存储库是每个请求的。因此,应使用每个请求的相同实例。除非对RedirectToAction的调用导致对我的索引操作的新请求。这将解释为什么我的AssetType属性为null。这是原因吗?我得出的结论正确吗?对RedirectToAction的调用确实会导致br我还检查了浏览器控制台,这是真的。这就回答了我的问题。