.net core EF Core 3.1.4导航属性在一对一关系中返回null

.net core EF Core 3.1.4导航属性在一对一关系中返回null,.net-core,entity-framework-core,.net Core,Entity Framework Core,我遇到了一个问题,在EF Core 3.1.4中,导航属性在一对一关系中总是返回null 我的模型结构如下: public class UserCredential { public Guid Id { get; set; } public string Username { get; set; } public string Password { get; set; } public byte[] Salt { g

我遇到了一个问题,在EF Core 3.1.4中,导航属性在一对一关系中总是返回null

我的模型结构如下:

    public class UserCredential
    {
        public Guid Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public byte[] Salt { get; set; }
        public bool IsLocked { get; set; }
        public bool IsDeleted { get; set; }   
        public DateTime CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }
        public DateTime? DeletedDate { get; set; }

        public UserProfile UserProfile { get; set; }
    }

    public class UserProfile
    {
        public Guid Id { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string Suffix { get; set; }
        public bool IsDeleted { get; set; }
        public List<Address> Addresses {get;set;}
        public DateTime DOB { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }
        public DateTime? DeletedDate { get; set; }

        public Guid UserCredentialId { get; set; }
        public UserCredential UserCredential { get; set; }
    }

然而,我的模型中的“UserProfile”总是空的

System.NullReferenceException:'对象引用未设置为 对象的实例。” IronCarp.Identity.Models.UserCredential.UserProfile.get返回null

我使用存储库与数据库交互,返回数据/模型的方法似乎足够简单

        private async Task<UserCredential> GetUserCredentials(string username)
        {
            return await context.UserCredentials.Where(u => u.Username == username).FirstOrDefaultAsync();
        }
专用异步任务GetUserCredentials(字符串用户名)
{
返回wait context.UserCredentials.Where(u=>u.Username==Username.FirstOrDefaultAsync();
}

感谢您的帮助,我不知道我错过了什么,谢谢

尝试在linkq中包含导航属性,尝试以下操作:

private async Task<UserCredential> GetUserCredentials(string username)
        {
            return await context.UserCredentials.Include(x => x.UserProfile).Where(u => u.Username == username).FirstOrDefaultAsync();
        }
专用异步任务GetUserCredentials(字符串用户名)
{
返回wait context.UserCredentials.Include(x=>x.UserProfile).Where(u=>u.Username==Username).FirstOrDefaultAsync();
}

对我来说,这个问题在我的集成测试中断断续续地发生

结果是因为我错误地将EF Core DbContext注册为
瞬态
,而不是
范围

Services.AddDbContext<SubscriptionsContext>(
            options => options.UseNpgsql(appConfig.DatabaseConnectionString,
                optionsBuilder => optionsBuilder.EnableRetryOnFailure(3)), ServiceLifetime.Transient);
Services.AddDbContext(
options=>options.UseNpgsql(appConfig.DatabaseConnectionString,
optionsBuilder=>optionsBuilder.EnableRetryOnFailure(3)),ServiceLifetime.Transient);
应该是:

Services.AddDbContext<SubscriptionsContext>(
            options => options.UseNpgsql(appConfig.DatabaseConnectionString,
                optionsBuilder => optionsBuilder.EnableRetryOnFailure(3)), ServiceLifetime.Scoped);
Services.AddDbContext(
options=>options.UseNpgsql(appConfig.DatabaseConnectionString,
optionsBuilder=>optionsBuilder.EnableRetryOnFailure(3)),ServiceLifetime.Scoped);
如果查看
AddDbContext()
扩展方法上的默认参数,它的作用域总是

Services.AddDbContext<SubscriptionsContext>(
            options => options.UseNpgsql(appConfig.DatabaseConnectionString,
                optionsBuilder => optionsBuilder.EnableRetryOnFailure(3)), ServiceLifetime.Transient);

是的,这很有效!非常感谢。如果我需要返回相关数据,我应该采用这种模式吗?我的印象是ef core会推断出这种关系并自动返回。我希望您阅读下面的文章,以了解如何快速加载。我是这样做的,但因为我的代表性较低,所以在我的代表性上升之前,它不会显示它。
Services.AddDbContext<SubscriptionsContext>(
            options => options.UseNpgsql(appConfig.DatabaseConnectionString,
                optionsBuilder => optionsBuilder.EnableRetryOnFailure(3)), ServiceLifetime.Scoped);