Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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# 模型属性返回null,但db MVC5 EF中存在值_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 模型属性返回null,但db MVC5 EF中存在值

C# 模型属性返回null,但db MVC5 EF中存在值,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在用MVC5学习EF6。我理解可能有人问过这个问题,但我不确定我到底需要寻找什么?我检查了数据库,数据在里面?谁能给我指出正确的方向吗 我有一个问题,在我的模型中。注册为空(此视图采用学生模型),但在数据库中它显示该表中有值 型号:学生 public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public ICollection

我正在用MVC5学习EF6。我理解可能有人问过这个问题,但我不确定我到底需要寻找什么?我检查了数据库,数据在里面?谁能给我指出正确的方向吗

我有一个问题,在我的模型中。注册为空(此视图采用学生模型),但在数据库中它显示该表中有值

型号:学生

public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
        public ICollection<Enrollment> Enrollments { get; set; }
public int ID { get; set; }
public decimal? Grade { get; set; }
public int StudentID { get; set; }
public int CourseID { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
  public DbSet<Student> Students { get; set; }
       public DbSet<Course> Courses { get; set; }
       public DbSet<Enrollment> Enrollments { get; set; }
       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
例外情况:

An exception of type 'System.NullReferenceException' occurred in App_Web_uycs14gs.dll but was not handled in user code
其他信息:对象引用未设置为对象的实例

数据库:

更新-上下文

public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
        public ICollection<Enrollment> Enrollments { get; set; }
public int ID { get; set; }
public decimal? Grade { get; set; }
public int StudentID { get; set; }
public int CourseID { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
  public DbSet<Student> Students { get; set; }
       public DbSet<Course> Courses { get; set; }
       public DbSet<Enrollment> Enrollments { get; set; }
       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
公共数据库集学生{get;set;}
公共数据库集课程{get;set;}
公共数据库集注册{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();

如果要延迟加载,则需要将导航属性更改为
虚拟

public class Student
{
   //...
   public virtual ICollection<Enrollment> Enrollments { get; set; }
}

public class Course
{
  //...
  public virtual ICollection<Enrollment> Enrollments { get; set; }
}

public class Enrollment
{
  //...
  public virtual Student Student { get; set; }
  public virtual Course Course { get; set; }
}
公共班级学生
{
//...
公共虚拟ICollection注册{get;set;}
}
公共课
{
//...
公共虚拟ICollection注册{get;set;}
}
公共班级招生
{
//...
公共虚拟学生学生{get;set;}
公共虚拟课程{get;set;}
}

检查此项以获得更好的解释。当您使用POCO实体类型时,通过创建派生代理类型的实例,然后覆盖
virtual
属性以添加加载挂钩,可以实现延迟加载。

请显示您的上下文。将注册设为虚拟,并使trylook与(同一个教程,同一个问题)@Rex请参阅更新的问题我在哪里将其设置为虚拟?哪个类?@dinAll您的导航properties@Harry,检查此项以查看您需要遵循的所有要求,这些要求似乎已经达到了目的。谢谢,我将详细阅读此项:)