Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用EF Core从MS SQL Server检索数据_C#_Asp.net Mvc_Asp.net Core_Entity Framework Core_Asp.net Identity - Fatal编程技术网

C# 无法使用EF Core从MS SQL Server检索数据

C# 无法使用EF Core从MS SQL Server检索数据,c#,asp.net-mvc,asp.net-core,entity-framework-core,asp.net-identity,C#,Asp.net Mvc,Asp.net Core,Entity Framework Core,Asp.net Identity,我正在使用ASP.NET Core 3.1、SQL Server 18、ASP.NET Identity 3.1.9和EF Core 3.1.9作为我大学课程的一个项目。对于我的项目,我采用代码优先的方法为数据库创建模型。其中一个模式是咨询 咨询。cs: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using Syst

我正在使用ASP.NET Core 3.1、SQL Server 18、ASP.NET Identity 3.1.9和EF Core 3.1.9作为我大学课程的一个项目。对于我的项目,我采用代码优先的方法为数据库创建模型。其中一个模式是咨询

咨询。cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace National_Healthcare_System.Models
{
    public class Consultation
    {
        [Key]
        [Display(Name = "Consultation ID")]
        public Guid Consultation_Id { get; set; }

        public Guid Doctor_Id { get; set; }

        [Required]
        [Display(Name = "Date and Time")]
        public DateTime Consultation_DateTime { get; set; } = DateTime.Now;

        [Display(Name = "NID/Birth Certificate No.")]
        [StringLength(13, ErrorMessage = "NID/Birth Certificate Number is Invalid Size", MinimumLength = 10)]
        public string Identity_Number { get; set; }

        [Required]
        [Display(Name = "Comment")]
        public string Comment { get; set; }

        [IgnoreDataMember]
        public virtual Doctor Doctor { get; set; }

        public virtual Users Users { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using National_Healthcare_System.Data;
using National_Healthcare_System.Models;

namespace National_Healthcare_System.Pages.Consultations
{
    public class IndexModel : PageModel
    {
        
        private readonly ApplicationDbContext _context;

        public IndexModel(ApplicationDbContext context)
        {
            _context = context;
        }

        public List<Consultation> Consultation { get; set; }

        public async Task OnGetAsync(Guid id)
        {
            Consultation = await _context.Consultation.ToListAsync();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using National_Healthcare_System.Data;
using National_Healthcare_System.Models;

namespace National_Healthcare_System.Pages.Consultations
{
    public class IndexModel : PageModel
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<IdentityUser> _userManager;
        private readonly SignInManager<IdentityUser> _signInManager;

        public IndexModel(UserManager<IdentityUser> userManager,
                          SignInManager<IdentityUser> signInManager,
                          ApplicationDbContext context)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _context = context;
        }

        #nullable enable
        public List<Consultation> Consultation { get; set; }

        public async Task OnGetAsync(IdentityUser user)
        {
            var userFromDb = await _context.User.FirstOrDefaultAsync(u => u.Email == user.Email);
            try 
            { 
                Consultation = await _context.Consultation.Where(c => c.Identity_Number == userFromDb.Identity_Number).ToListAsync();
            }
            catch { Exception ex; }
        }
    }    
}
我有这个模型的CRUD页面。现在,在“Index.cshtml”页面中,我想显示只有“当前用户”才有的协商。“身份号码”是
咨询
表中的FK,用于与
用户
表建立关系。我跟随的教程自动生成了CRUD页面,我也在互联网上寻找了更多的资料,但它们对我没有多大帮助

自动生成的Index.cshtml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace National_Healthcare_System.Models
{
    public class Consultation
    {
        [Key]
        [Display(Name = "Consultation ID")]
        public Guid Consultation_Id { get; set; }

        public Guid Doctor_Id { get; set; }

        [Required]
        [Display(Name = "Date and Time")]
        public DateTime Consultation_DateTime { get; set; } = DateTime.Now;

        [Display(Name = "NID/Birth Certificate No.")]
        [StringLength(13, ErrorMessage = "NID/Birth Certificate Number is Invalid Size", MinimumLength = 10)]
        public string Identity_Number { get; set; }

        [Required]
        [Display(Name = "Comment")]
        public string Comment { get; set; }

        [IgnoreDataMember]
        public virtual Doctor Doctor { get; set; }

        public virtual Users Users { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using National_Healthcare_System.Data;
using National_Healthcare_System.Models;

namespace National_Healthcare_System.Pages.Consultations
{
    public class IndexModel : PageModel
    {
        
        private readonly ApplicationDbContext _context;

        public IndexModel(ApplicationDbContext context)
        {
            _context = context;
        }

        public List<Consultation> Consultation { get; set; }

        public async Task OnGetAsync(Guid id)
        {
            Consultation = await _context.Consultation.ToListAsync();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using National_Healthcare_System.Data;
using National_Healthcare_System.Models;

namespace National_Healthcare_System.Pages.Consultations
{
    public class IndexModel : PageModel
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<IdentityUser> _userManager;
        private readonly SignInManager<IdentityUser> _signInManager;

        public IndexModel(UserManager<IdentityUser> userManager,
                          SignInManager<IdentityUser> signInManager,
                          ApplicationDbContext context)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _context = context;
        }

        #nullable enable
        public List<Consultation> Consultation { get; set; }

        public async Task OnGetAsync(IdentityUser user)
        {
            var userFromDb = await _context.User.FirstOrDefaultAsync(u => u.Email == user.Email);
            try 
            { 
                Consultation = await _context.Consultation.Where(c => c.Identity_Number == userFromDb.Identity_Number).ToListAsync();
            }
            catch { Exception ex; }
        }
    }    
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Identity;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.EntityFrameworkCore;
使用国家医疗保健系统数据;
使用国家医疗保健系统模型;
名称空间National_Healthcare_System.Pages.Consultations
{
公共类索引模型:PageModel
{
私有只读应用程序的bContext\u上下文;
公共索引模型(ApplicationDbContext上下文)
{
_上下文=上下文;
}
公共列表咨询{get;set;}
公共异步任务OnGetAsync(Guid id)
{
Consultation=wait_context.Consultation.ToListAsync();
}
}
}
我试图修改它,以便仅获取当前用户的数据

修改的Index.cshtm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

namespace National_Healthcare_System.Models
{
    public class Consultation
    {
        [Key]
        [Display(Name = "Consultation ID")]
        public Guid Consultation_Id { get; set; }

        public Guid Doctor_Id { get; set; }

        [Required]
        [Display(Name = "Date and Time")]
        public DateTime Consultation_DateTime { get; set; } = DateTime.Now;

        [Display(Name = "NID/Birth Certificate No.")]
        [StringLength(13, ErrorMessage = "NID/Birth Certificate Number is Invalid Size", MinimumLength = 10)]
        public string Identity_Number { get; set; }

        [Required]
        [Display(Name = "Comment")]
        public string Comment { get; set; }

        [IgnoreDataMember]
        public virtual Doctor Doctor { get; set; }

        public virtual Users Users { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using National_Healthcare_System.Data;
using National_Healthcare_System.Models;

namespace National_Healthcare_System.Pages.Consultations
{
    public class IndexModel : PageModel
    {
        
        private readonly ApplicationDbContext _context;

        public IndexModel(ApplicationDbContext context)
        {
            _context = context;
        }

        public List<Consultation> Consultation { get; set; }

        public async Task OnGetAsync(Guid id)
        {
            Consultation = await _context.Consultation.ToListAsync();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using National_Healthcare_System.Data;
using National_Healthcare_System.Models;

namespace National_Healthcare_System.Pages.Consultations
{
    public class IndexModel : PageModel
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<IdentityUser> _userManager;
        private readonly SignInManager<IdentityUser> _signInManager;

        public IndexModel(UserManager<IdentityUser> userManager,
                          SignInManager<IdentityUser> signInManager,
                          ApplicationDbContext context)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _context = context;
        }

        #nullable enable
        public List<Consultation> Consultation { get; set; }

        public async Task OnGetAsync(IdentityUser user)
        {
            var userFromDb = await _context.User.FirstOrDefaultAsync(u => u.Email == user.Email);
            try 
            { 
                Consultation = await _context.Consultation.Where(c => c.Identity_Number == userFromDb.Identity_Number).ToListAsync();
            }
            catch { Exception ex; }
        }
    }    
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Identity;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.EntityFrameworkCore;
使用国家医疗保健系统数据;
使用国家医疗保健系统模型;
名称空间National_Healthcare_System.Pages.Consultations
{
公共类索引模型:PageModel
{
私有只读应用程序的bContext\u上下文;
私有只读用户管理器_UserManager;
专用只读签名管理器\u签名管理器;
公共索引模型(UserManager UserManager,
SignInManager SignInManager,
ApplicationDbContext(上下文)
{
_userManager=userManager;
_signInManager=signInManager;
_上下文=上下文;
}
#可空启用
公共列表咨询{get;set;}
公共异步任务OnGetAsync(IdentityUser用户)
{
var userFromDb=wait _context.User.FirstOrDefaultAsync(u=>u.Email==User.Email);
尝试
{ 
Consultation=wait_context.Consultation.Where(c=>c.Identity_Number==userFromDb.Identity_Number.toListSync();
}
catch{Exception ex;}
}
}    
}
不带
Try Catch
的代码将抛出此错误:

NullReferenceException:对象引用未设置为对象的实例。 lambda_法(闭包法)

InvalidOperationException:尝试计算LINQ查询参数表达式时引发异常。要显示其他信息,请在重写DbContext.onconfigurang时调用EnableSensitiveDataLogging()。 Microsoft.EntityFrameworkCore.Query.Internal.ParameterExtractionExpressionVisitor.GetValue(表达式表达式,输出字符串parameterName)]

截图:[]

尝试了
#nullable enable
,因为我认为如果咨询表为空,它会返回异常,但后来我在表中输入数据时,它仍然是一样的


我想使用Identity core读取当前用户。我真的不知道它是否是这样工作的,也不知道我如何才能实现只阅读当前用户咨询数据的目标?最近我在学习C#,ASP.NET核心和实体框架。所以我对它们了解不多,只是突然尝试了这些修改。

多亏了@PaulCarlton和@Romka,我现在知道问题出在哪里了。帮助我找到解决这个问题的办法。我不得不进一步修改我的代码:

public async Task OnGetAsync()
        {
            //this line helped to get current users data from DB
var user = await _userManager.GetUserAsync(HttpContext.User);
            var userFromDb = await _context.User.FirstOrDefaultAsync(u => u.Email == user.Email);
                     Consultation = await _context.Consultation.Where(c => c.Identity_Number == userFromDb.Identity_Number).ToListAsync();  
        }
此线程非常有用:

数据库中是否有任何数据?在尝试从空对象提取数据之前,请确保该对象不为空。代码似乎没有在数据库中找到用户。在调试过程中,在第行“Consultation=await\u context.Consultation.Where…”处使用断点可以看到,如果对象“userFromDb”的Email属性为null,则有。我已插入当前用户的数据。当前用户的身份号码在那里。但是错误仍然存在。是的,“userFromDb”似乎为空。那么我如何检索当前用户的身份号码?Identity_Number是我使用Users类创建的自定义属性,并与.Net Identity一起使用@Romka
#可空启用
与您的问题无关。可空上下文允许通过显式地将对象声明为可空或不可空来设计其契约(接口)。这是一个设计决定。