简体   繁体   English

急切加载包括使用问题

[英]Eager Loading Include use issue

I am very new to MVC and Entity Framework and am trying to follow the asp.net tutorials and have run into a major snag. 我对MVC和Entity Framework还是很陌生,并且正在尝试遵循asp.net教程并遇到重大障碍。 I have a table that displays correctly but shows the foreign key in the table instead of the data from the reference table. 我有一个表,可以正确显示,但是在表中显示外键,而不是参考表中的数据。 I think I am wanting to utilize eager loading here but I am not sure where to place my include. 我想我想在这里利用热切的加载,但是我不确定将include放在哪里。 I believe the tutorial on asp.net is for an older version of MVC. 我相信asp.net上的教程适用于旧版本的MVC。 I understand that my include should go in the following snippet. 我了解我的include应该放在以下片段中。 I am trying to include the artist type. 我试图包括艺术家类型。

First, part of my model class: 首先,我模型课程的一部分:

        modelBuilder.Entity<tblArtist>()
            .HasMany(e => e.tblArtisttblArtistType)
            .WithRequired(e => e.tblArtist)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<tblArtist>()
            .HasMany(e => e.tblIssueArtist)
            .WithRequired(e => e.tblArtist)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<tblArtist>()
            .HasMany(e => e.tblIssueAutograph)
            .WithRequired(e => e.tblArtist)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<tblArtistType>()
            .HasMany(e => e.tblArtisttblArtistType)
            .WithRequired(e => e.tblArtistType)
            .WillCascadeOnDelete(false);

And my controller code: 而我的控制器代码:

public class tblArtistsController : Controller
{
    private joerdieComicsModel1 db = new joerdieComicsModel1();

    // GET: tblArtists
    public ActionResult Index()
    {

        return View(db.tblArtist.ToList());
    }

    // GET: tblArtists/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        tblArtist tblArtist = db.tblArtist.Find(id);
        if (tblArtist == null)
        {
            return HttpNotFound();
        }
        return View(tblArtist);
    }

I understand that the include code should go in the public ActionResult Index() method but I am lost as to how that is done. 我知道包含代码应该放在公共ActionResult Index()方法中,但是我不知道该怎么做。

Thank you. 谢谢。

When you have Many to Many relation Entity Framework automatically Create Table Interface but to relations that have foreign key you model must "EX : ArtistId". 当您具有多对多关系时,实体框架会自动创建表接口,但是对于具有外键的关系,您必须建模“ EX:ArtistId”。

For Example : Person.cs 例如: Person.cs

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

    public string Name { get; set; }

    public ICollection<Book> Books { get; set; }
  }

Book.cs Book.cs

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

    public string Name { get; set; }

    public Person Person { get; set; }

    public int PersonId { get; set; }
}

In Model Builder : 在模型构建器中:

 modelBuilder.Entity<Person>()
             HasMany(_ => _.Books)
            .WithRequired(_ => _.Person)
            .HasForeignKey(_ => _.PersonId)
            .WillCascadeOnDelete();

And See : One To Many 看: 一对多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM