Asp.net mvc 4 ASP.NET MVC4和CodeFirst中的一对多关系

Asp.net mvc 4 ASP.NET MVC4和CodeFirst中的一对多关系,asp.net-mvc-4,ef-code-first,entity-framework-migrations,Asp.net Mvc 4,Ef Code First,Entity Framework Migrations,我在ASP.NET MVC4和CodeFirst的关系方面遇到了一些问题,这些表返回的值与外键相关 首先,让我们看看我做得是否正确 下面是我的代码示例: 人类 城市级 因此,有了这个,数据库创建了一个好看的关系,并且看起来工作得很好。在这段代码之后,我有如下表格: 人 --Id(主键) --名称 --姓氏 --城市标识(FK) 城市 --Id(主键) --名字 我用一个种子填充了它,下面是一个例子: context.Person.AddOrUpdate(p => p.Name, n

我在ASP.NET MVC4和CodeFirst的关系方面遇到了一些问题,这些表返回的值与外键相关

首先,让我们看看我做得是否正确

下面是我的代码示例:

人类 城市级 因此,有了这个,数据库创建了一个好看的关系,并且看起来工作得很好。在这段代码之后,我有如下表格:


--Id(主键)
--名称
--姓氏
--城市标识(FK)

城市
--Id(主键)
--名字

我用一个种子填充了它,下面是一个例子:

context.Person.AddOrUpdate(p => p.Name,
    new Person { Name = "Me", City = new City { Name = "Ludlow" } }
);
当我需要检索视图中的信息时,就像这样

MyDataBase.cs

public class LeilaoDb : DbContext
{
    public DbSet<Person> Persons { get; set; }
    public DbSet<City> Cities { get; set; }
}
MyDataBase _db = new MyDataBase();

        public ActionResult Index()
        {
            var model = _db.Persons.ToList();

            return View(model);
        }
Home/Index.cshtml

@model IEnumerable<testingproject.Models.Person>

@{
    ViewBag.Title = "Home Page";
}

@foreach (var item in Model)
{
    @Html.Partial( "_Person", item );
}
@model testingproject.Models.Person

<div>
    <h3>@Model.Name</h3>
    @Model.City.Name
</div>
那么,怎么了

解决方案: 我找到了解决办法

MyDataBase _db = new MyDataBase();

public ActionResult Index()
{
    var model = _db.Persons.ToList();

    return View(model);
}
这段代码只检索人员,可能是为了在不需要处理关系时不过载。您需要指定何时需要使用
Include()
方法处理这些关系

在此之后,很简单:

MyDataBase _db = new MyDataBase();

public ActionResult Index()
{
    var model = _db.Persons.Include("City");

    return View(model);
}
我觉得给这个方法传递一个字符串很奇怪,但没关系。如果我真的需要,我现在可以使用
@Model.City.Name
返回我的值


我在这个网站上找到了解决方案

实体框架并没有不必要的关系。如果您想包含others表,则需要调用一个方法或将属性设置为虚拟,以建立惰性关系

公共虚拟城市{get;set;}

这样,就形成了惰性模式

var模型=_db.Persons.Include(“城市”).ToList()

这种方法是在必要时手动包含关系表的方法


如果您只想呼叫一个人而不进行连接,只需调用
\u db.Persons.ToList()

这是因为您明确地使用了
模型。如果@model没有收到任何数据,则
model
将为
null
。最好使用帮助程序,如
@Html.EditorFor(model=>model.Name)
。这将处理
null
的情况。是否将模型传递给视图?我将模型传递给视图,如下所示:
public ActionResult Index(){var model=_db.Persons.ToList();return view(model)}
在这种情况下,我有一个
MyDataBase\u db=new MyDataBase()
我有一个上下文是
MyDataBase.cs
,在这个.cs里面,我有:
public-DbSet-Persons{get;set;}
public-DbSet-Cities{get;set;}
对不起,我有另一个文件是:
@model-IEnumerable
在这个
@foreach(模型中的var-item){@Html.Partial('u-Person',item)}
。而且,如果我删除
@Model.City.Name
,视图将返回此人的姓名。由于您正在为属性名称
City
指定一个类
City
的实例,因此可能存在一些歧义。。。
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
MyDataBase _db = new MyDataBase();

public ActionResult Index()
{
    var model = _db.Persons.ToList();

    return View(model);
}
MyDataBase _db = new MyDataBase();

public ActionResult Index()
{
    var model = _db.Persons.Include("City");

    return View(model);
}