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
Asp.net mvc 实体框架导航属性_Asp.net Mvc_Entity Framework_Navigation Properties - Fatal编程技术网

Asp.net mvc 实体框架导航属性

Asp.net mvc 实体框架导航属性,asp.net-mvc,entity-framework,navigation-properties,Asp.net Mvc,Entity Framework,Navigation Properties,我目前正在尝试开发我的第一个.NETMVC应用程序,并学习主要概念 在我的应用程序中,我有一个表,显示动物表中的动物列表。在表中,我还试图显示动物品种,但我从动物表中存储的外键上的品种表中提取该品种 我目前正在尝试使用导航属性来显示品种文本,而不是ID,因此我 把我的动物模型改成这样 public partial class Animal { public int AnimalId { get; set; } public Nullable<int> UserId {

我目前正在尝试开发我的第一个.NETMVC应用程序,并学习主要概念

在我的应用程序中,我有一个表,显示动物表中的动物列表。在表中,我还试图显示动物品种,但我从动物表中存储的外键上的品种表中提取该品种

我目前正在尝试使用导航属性来显示品种文本,而不是ID,因此我 把我的动物模型改成这样

public partial class Animal
{
    public int AnimalId { get; set; }
    public Nullable<int> UserId { get; set; }
    public string TagNo { get; set; }
    public Nullable<int> Species { get; set; }
    public Nullable<bool> Sex { get; set; }
    public Nullable<int> AnimalBreed { get; set; }
    public Nullable<System.DateTime> DOB { get; set; }
    public Nullable<int> OwnershipStatus { get; set; }
    public Nullable<System.DateTime> DateAdded { get; set; }
    public Nullable<bool> BornOnFarm { get; set; }

    public virtual Breed Breed { get; set; }
}
我的品种模型看起来像

public partial class Breed
{
    public int id { get; set; }
    public Nullable<int> SpeciesID { get; set; }
    public string Breed1 { get; set; }
}
在我看来,我试图显示我的动物模型中的“品种”字段,如下所示,但“品种”列只是空的

<td>
  @Html.DisplayFor(modelItem => item.Breed.Breed1)
</td>
最后,这里是我用来将模型发送到视图的代码

List<Animal> Animal1 = (from animals in db.Animals
    where animals.Species == 2 && animals.OwnershipStatus == 1
    && animals.UserId == WebSecurity.CurrentUserId
    select animals).ToList();

return View(Animal1);
如果DbContext中未启用延迟加载,则必须显式加载或使用渴望加载导航属性

你最终会得到这样的结果:

var res = (from animals in db.Animals.Include("Breeds")
                                    where animals.Species == 2 & animals.OwnershipStatus == 1
                                    & animals.UserId == WebSecurity.CurrentUserId
                                    select animals).ToList();
@model Namespace.To.Breed
@Html.DisplayFor(m => m.Breed1)
如果DbContext中未启用延迟加载,则必须显式加载或使用渴望加载导航属性

你最终会得到这样的结果:

var res = (from animals in db.Animals.Include("Breeds")
                                    where animals.Species == 2 & animals.OwnershipStatus == 1
                                    & animals.UserId == WebSecurity.CurrentUserId
                                    select animals).ToList();
@model Namespace.To.Breed
@Html.DisplayFor(m => m.Breed1)

首先,不要将单个项目多元化。这会造成代码混乱:

public virtual Breed Breed { get; set; }
-或-

有另一种方法,但它更复杂,而且对于这个场景来说可能是杀伤力过大。如果你真的想直接使用品种,那么你需要为品种定义一个显示模板。您可以通过向Views\Shared named DisplayTemplates添加一个新文件夹来完成此操作。在该文件夹中,添加一个名为bride.cshtml的视图。此处视图的名称对应于类名,而不是属性名。在该视图中,您可以执行以下操作:

var res = (from animals in db.Animals.Include("Breeds")
                                    where animals.Species == 2 & animals.OwnershipStatus == 1
                                    & animals.UserId == WebSecurity.CurrentUserId
                                    select animals).ToList();
@model Namespace.To.Breed
@Html.DisplayFor(m => m.Breed1)
然后,在你看来,你可以做:

@Html.DisplayFor(m => m.Breed)

Razor将使用显示模板渲染适当的内容。正如我所说的,这有点过分了,但在更复杂的对象渲染中,它可能会派上用场。

首先,不要将单个项目多元化。这会造成代码混乱:

public virtual Breed Breed { get; set; }
-或-

有另一种方法,但它更复杂,而且对于这个场景来说可能是杀伤力过大。如果你真的想直接使用品种,那么你需要为品种定义一个显示模板。您可以通过向Views\Shared named DisplayTemplates添加一个新文件夹来完成此操作。在该文件夹中,添加一个名为bride.cshtml的视图。此处视图的名称对应于类名,而不是属性名。在该视图中,您可以执行以下操作:

var res = (from animals in db.Animals.Include("Breeds")
                                    where animals.Species == 2 & animals.OwnershipStatus == 1
                                    & animals.UserId == WebSecurity.CurrentUserId
                                    select animals).ToList();
@model Namespace.To.Breed
@Html.DisplayFor(m => m.Breed1)
然后,在你看来,你可以做:

@Html.DisplayFor(m => m.Breed)

Razor将使用显示模板渲染适当的内容。正如我所说,这太过分了,但在更复杂的对象渲染中,它可能会派上用场。

我仍然不知道如何加载上面的查询。我必须将Include合并到select?我仍然不确定如何加载上面的查询。我必须把Include加入select??嗨,克里斯。谢谢你的帮助,但我还没有解决这个问题。我修改了我的代码,并调整了上面的问题以反映它。在接受了你的建议后,我仍然遇到同样的问题。它仍然显示一个空白列。我做错什么了吗?谢谢你确定Breed1不是空白的?您的实体上不需要它,所以它很容易为NULL或空字符串。我刚刚检查了我的品种表,所有行中的所有列都包含数据?嗨,克里斯。谢谢你的帮助,但我还没有解决这个问题。我修改了我的代码,并调整了上面的问题以反映它。在接受了你的建议后,我仍然遇到同样的问题。它仍然显示一个空白列。我做错什么了吗?谢谢你确定Breed1不是空白的?您的实体不需要它,所以它很容易为NULL或空字符串。我只是检查了我的繁殖表,所有行中的所有列都包含数据?