Why the include function is not including the navigational property of the model?

c# ef-code-first entity-framework entity-framework-6 model-view-controller

Question

I have these 2 models:

Phones:

public class Phones
    {
        public int ID { get; set; }
        public string Model { get; set; }
        public string Manufacturer { get; set; } // e.g. XIAOMI, NOKIA, IPHONE    
        public int UserID { get; set; }
        public List<Users> Users { get; set; }
    }

Users:

 public class Users
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string CNIC { get; set; }
    }

Context model:

    public class UsersPhonesDBContext: DbContext
    {
        public DbSet<Users> Users { get; set; }
        public DbSet<Phones> Phones { get; set; }
    }

and controller:

        public ActionResult PhoneUsers()
        {
            Object ObjPhoneUsers = context.Phones.Include(phone => phone.Users).FirstOrDefault().Users ;

            return View(ObjPhoneUsers);
        }

And View:

    @model IEnumerable<PracticeEF.Models.Phones>

    <table>
    @foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Model)

        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Manufacturer)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserID)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

Problem:

The problem is that it returns the navigational property (Users list in Phones model) being empty. Even the records are filled and being mapped via the foreign key in the database but it doesn't pick any record of the user but only Phones.

Why?

Update:

I even tried this and returns nothing, nor the users nor the phones.

 public ActionResult PhoneUsers()
        {
            object ObjPhoneUsers = context.Phones.Where(x => x.Manufacturer == "Apple Inc").Include(x => x.Users).ToList();

            return View(ObjPhoneUsers);
        }
1
0
1/12/2019 11:01:34 AM

Accepted Answer

If you want to create an one-to-many relationship between those two entities your model would be like this:

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

    //navigation property
    public ICollection<Image> ScrollerImages {get;set;}
}

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

    //FK
    public int? PageConfigId {get;set;}

    //navigation property
    public PageConfig PageConfig {get;set;}
}

And the Fluent Api configuration would be:

modelBuilder.Entity<Image>()
            .HasOptional(i=>i.PageConfig)
            .WithMany(pc=>pc.ScrollerImages)
            .HasForeignKey(i=> i.PageConfigId);

If you idea is create an unidirectional one-to-many relationship then delete the FK and the navigation property on Image entity and configure the relationship this way:

modelBuilder.Entity<PageConfig>()
            .HasMany(pc => pc.ScrollerImages)
            .WithOptional();

Check this link for more info about this kind of relationship

8
5/12/2015 10:43:33 PM

Popular Answer

Per http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx:

"...you can use Fluent API to configure a One-to-Many relationship using Student entity classes as shown below."

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
            //one-to-many 
            modelBuilder.Entity<Student>()
                        .HasRequired<Standard>(s => s.Standard)
                        .WithMany(s => s.Students)
                        .HasForeignKey(s => s.StdId);

    }

"...Use HasOptional method instead of HasRequired method to make foreign key column nullable."

So you'd be looking for something like this:

modelBuilder.Entity<Image>()
            .HasOptional<PageConfig>(x => x.PageConfig)
            .WithMany(x => x.ScrollerImages)
            .HasForeignKey(x => x.PageConfigId)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow