Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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_C#_Asp.net Mvc - Fatal编程技术网

C# 导航属性返回NULL

C# 导航属性返回NULL,c#,asp.net-mvc,C#,Asp.net Mvc,我正在尝试打印索引视图中每个人的Person.bank1.Name,但始终为空。我先用代码。在数据库表中,一切正常 背景是: public class PersonContext : DbContext { public PersonContext() : base("name=PersonContext") { Configuration.ProxyCreationEnabled = true; Configuration.Laz

我正在尝试打印索引视图中每个人的Person.bank1.Name,但始终为空。我先用代码。在数据库表中,一切正常

背景是:

public class PersonContext : DbContext
{
    public PersonContext()
        : base("name=PersonContext")
    {
        Configuration.ProxyCreationEnabled = true;
        Configuration.LazyLoadingEnabled = true;
    }

    public virtual DbSet<Bank> banks { get; set; }
    public virtual DbSet<Person> person { get; set; }
}
银行模式:

public class Bank
{
    public int Id {get;set;}

    public string Name { get; set; }

    public virtual ICollection<Person> person { get; set; }
}

在Person类中将bank int属性声明为外键-

[ForeignKey("Bank1")]
public int bank { get; set; }

这应该可以做到。

在Person类中将bank int属性声明为外键-

[ForeignKey("Bank1")]
public int bank { get; set; }

这应该可以解决问题。

您是否在您的上下文中的数据库或“OnModelCreating”函数中为个人银行创建了外键您是否在您的上下文中的数据库或“OnModelCreating”函数中为个人银行创建了外键
[ForeignKey("Bank1")]
public int bank { get; set; }