none
Entity Framework initial eager loading one to many relationship RRS feed

  • Question


  • I did my research on this forum before posting this but I couldn't find anything that addressed my specific question. I have an entity framework application that I was originally using lazy loading.  I need to serialize all of my entities so I need to turn off lazy loading and proxy creation (as shown in the code below).  Is there a way to use eager loading to load all my entities at one time when the application starts (from what I've read this would not be possible in the OnModelCreating method in my DbContext)?  Is this something I can do in my ViewModel.cs constructor?

    I have a Customers ObservableCollection which has many Project ObservableCollections, which has many Program ObservableCollection, which has many Revisions ObservableCollection.  To load all my data can I use something like this?

       var q = context.Customers.Include(
                    "Customers.Projects.Programs.Revisions");


     public partial class RevisionModelContainer : DbContext
        {
            public RevisionModelContainer()
                : base("name=RevisionModelContainer")
            {
                this.Configuration.ProxyCreationEnabled = false;
                this.Configuration.LazyLoadingEnabled = false;
            }
        
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                throw new UnintentionalCodeFirstException();
            }
        
            public DbSet<Customer> Customers { get; set; }
            public DbSet<Project> Projects { get; set; }
            public DbSet<Program> Programs { get; set; }
            public DbSet<Revision> Revisions { get; set; }
        }



    ViewModel.cs

    public class ViewModel:INotifyPropertyChanged
        {
            RevisionModelContainer context = new RevisionModelContainer();
            
            public ViewModel()
            {
                Customers = new ObservableCollection<Customer>();
                var q = context.Customers.Include(
                    "Customers.Projects.Programs.Revisions").ToList();
    
                foreach (var i in q)
                {
                    Customers.Add(i);
                }
            }
    
            private void UpDate()
            {
               
                    Customers.Clear();
                    //foreach (var customer in context.Customers.OrderBy(c => c.Name))
                    //{
                    //    Customers.Add(customer);
                       
                    //} 
    
                    var q = context.Customers.Include("Customers.Projects.Programs.Revisions").ToList();
                    foreach (var i in q)
                    {
                        Customers.Add(i);
                    }
            }
    
            #region Add new customer,project,program,rev methods
    
            public void AddCustomer(string customerName)
            {
                try
                {
                    Customer customer = new Customer { Name = customerName };
                    this.Customers.Add(customer);
                    context.Customers.Add(customer);
                    context.SaveChanges();
                    UpDate();
                }
    
                catch (NullReferenceException nullException)
                {
                    MessageBox.Show(nullException.Message);
                }
    
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            public void AddProject(string customerName, string projectName)
            {  
                    var customer = context.Customers.First(c => c.Name == customerName);
                    customer.Projects.Add(new Project { Name = projectName });
                    context.SaveChanges();
                    UpDate();
            }
    
            public void AddProgram(string projectName, string programName)
            {  
                    var project = context.Projects.First(c => c.Name == projectName);
                    project.Programs.Add(new Program { Name = programName });
                    context.SaveChanges();
                    UpDate();   
            }
    
            public void AddRevision(string programName, string revisionName,string notes)
            {     
                    var program = context.Programs.First(c => c.Name == programName);
                    program.Revisions.Add(new Revision
                    {
                        Name = revisionName,
                        Date = DateTime.Now,
                        Notes = notes
                    });
                    context.SaveChanges();       
            }
            #endregion
    
            #region Binding properties
    
            private ObservableCollection<Customer> customers;
            public ObservableCollection<Customer> Customers
            {
                get { return customers; }
                set
                {
                    customers = value;
                    OnPropertyChanged("Customers");               
                }
            }
    
            private ObservableCollection<Project> projects;
            public ObservableCollection<Project> Projects 
            {
                get { return projects; }
                set
                {
                    projects = value;
                    OnPropertyChanged("Projects");
                }
            }
    
            private ObservableCollection<Program> programs;
            public ObservableCollection<Program> Programs 
            {
                get { return programs; }
                set
                {
                    programs = value;
                    OnPropertyChanged("Programs");
                }
            }
    
            private ObservableCollection<Revision> revisions;
            public ObservableCollection<Revision> Revisions 
            {
                get { return revisions; }
                set
                {
                    revisions = value;
                    OnPropertyChanged("Revisions");
                }
            }
    
            private DateTime dates;
            public DateTime Dates 
            {
                get { return dates; }
                set
                {
                    dates = value;
                    OnPropertyChanged("Dates");
                }
            }
    
            private string notes;
            public string Notes 
            {
                get { return notes; }
                set
                {
                    notes = value;
                    OnPropertyChanged("Notes");
                }
            }


    Tuesday, March 25, 2014 3:04 PM

Answers

  • Hello,

    >>To load all my data can I use something like this?

    It does not need to include the Customer again, we just need to include other three one:

    using (MFDBEntities db = new MFDBEntities())
    
                {
    
                    db.Configuration.LazyLoadingEnabled = false;
    
                    db.Configuration.ProxyCreationEnabled = false;
    
                    List<Customers> Cs = db.Customers.Include("Projects.ProgramXes.Revisions").ToList();
    
                }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 26, 2014 3:49 AM
    Moderator