简体   繁体   中英

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. 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. I believe the tutorial on asp.net is for an older version of MVC. I understand that my include should go in the following snippet. 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.

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".

For Example : Person.cs

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

    public string Name { get; set; }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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