Entity Framework nesting classes inside class

asp.net-mvc-5 c# class entity-framework-6 model

Question

I trying to make a data model that allows users to add companies to a list and then when added, allow further details on the company to be added, this is to allow things like employee information to be added. However, I haven't been able to figure out how to do this without creating multiple class, controller files and view folders.

My current code on the model class file is:

namespace Intranet.Models
{
    public class AddCustomers
    {
        public int AddCustomersID { get; set; }
        public string CompanyName { get; set; }
        ...

        public class EmployeeInfo
        {
            public int EmployeeInfoID { get; set; }
            public string Forename { get; set; }
            ...
        }

        public class ContactInfo
        {
            public int ContactInfoID { get; set; }
            public string Code { get; set; }
            ...
        }
    }
}

Context file:

public IntranetContext() : base("name=IntranetContext")
{
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}

public System.Data.Entity.DbSet<Intranet.Models.AddCustomers> AddCustomers { get; set; }
public System.Data.Entity.DbSet<Intranet.Models.AddCustomers.EmployeeInfo> EmployeeInfo { get; set; }
public System.Data.Entity.DbSet<Intranet.Models.AddCustomers.ContactInfo> ContactInfo { get; set; }

This does not work however as the nested classes do not appear anywhere, except in my local database.

Is there a way I can get the nested classes to appear in the details view for each customer? Or is there a completely different way to do this?

1
0
11/17/2017 3:58:22 PM

Accepted Answer

namespace Intranet.Models
{
    public class AddCustomers
    {
        public int AddCustomersID { get; set; }
        public string CompanyName { get; set; }
        public EmployeeInfo EmployeeInfo { get; set; }
        public ContactInfo ContactInfo { get; set; }
        ...
    }

    public class EmployeeInfo
    {
        public int EmployeeInfoID { get; set; }
        public string Forename { get; set; }
        ...
    }

    public class ContactInfo
    {
        public int ContactInfoID { get; set; }
        public string Code { get; set; }
        ...
    }        
}

Then in your context you would have:

public System.Data.Entity.DbSet<Intranet.Models.AddCustomers> AddCustomers { get; set; }
public System.Data.Entity.DbSet<Intranet.Models.EmployeeInfo> EmployeeInfo { get; set; }
public System.Data.Entity.DbSet<Intranet.Models.ContactInfo> ContactInfo { get; set; }

..and in controller you could do things like:

var customer = context.AddCustomers
    .FirstOrDefault(c=>c.EmployeeInfo.Forename=="bob");
...
0
11/17/2017 4:44:19 PM


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