none
Cannot get code to recognize the related data entity in MVC database first project RRS feed

  • Question

  •  I am using C#, VS 2010 and database first approach to building a MVC3 application.  When looking at the various classes please note the namespaces and using statements.
     
    The code block I pasted below works as expected.  However, I want to load related data (in this case Addresses) when loading the subject.  (See the class EFDbContext class below).  In the physical database, tblPerson has a 1-many relation with tblAddress.
     
    In SubjectRepository class GetSubject method I want to change this line:
     
    return context.Subjects.Where(p => p.Email == userName && p.webPassword == passWord).Single();
    
    //I want to load the related Addresses of Subjects
    
    return context.Subjects.Include(a => a.Addresses).Where(p => p.Email == userName && p.webPassword == passWord).Single();
    
      First, I know something is wrong because intellisense does not provide Addresse as a property of a.  The error message I am seeing is:  Cannot convert lamda expression to type 'string' because it is not a delgate type.
    If changes from the lamba expression to simple Addresses, I get the intellesense message:  Addresses does not exist in the current context.
    Any help is appreciated.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using CompositeAppointment.Domain.Entities;
    using CompositeAppointment.Domain.Abstract;
    
    namespace CompositeAppointment.Domain.Concrete 
    {
        public class SubjectRepository : ISubjectRepository
        {
            private EFDbContext context = new EFDbContext();
    
           public Subject GetSubject(string userName, string passWord) 
            {
                
                return context.Subjects.Where(p => p.Email == userName && p.webPassword == passWord).Single();
            }
    
           public void SaveSubject(Subject subject)
           {
               if (subject.PersonID == 0)
               {
                   context.Subjects.Add(subject);
               }
               else
               {
                   context.Entry(subject).State = EntityState.Modified;
               }
               context.SaveChanges();
           }
     
        }
    }
     Below is context followed by all other classes involved
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using CompositeAppointment.Domain.Entities;
    
    namespace CompositeAppointment.Domain.Concrete
    {
        public class EFDbContext : DbContext
        {
            public DbSet<Subject> Subjects { get; set; }
    
            //public DbSet<Associate> Associates { get; set; }
    
            public DbSet<Address> Addresses { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Subject>().Map(m => { m.ToTable("tblPersonComp"); });
                //modelBuilder.Entity<Associate>().Map(m => { m.ToTable("tblPersonComp"); });
               modelBuilder.Entity<Address>().Map(m => { m.ToTable("tblAddress"); });
               modelBuilder.Entity<Subject>().HasMany(a => a.Addresses);
            }
        }
    
    }
    


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    
    namespace CompositeAppointment.Domain.Entities
    {
        public class Subject
        {
            [Key]
            [Column("Person_ID")]
            public int PersonID { get; set; }
    
            [Column("Org_ID")]
            public int orgID { get; set; }
    
            [Column("DayDT")]
            public DateTime ?apptDay { get; set; }
    
            [Column("StartTime24")]
            public string startTime { get; set; }
    
            public string Email { get; set; }
    
            [Column("WebPassword")]
            public string webPassword { get; set; }
    
            [Column("Officer")]
            public string officerTitle { get; set; }
    
            [Column("PledgeClass")]
            public string pledgeClass { get; set; }
    
            [Column("FirstName")]
            public string firstName { get; set; }
    
            [Column("LastName")]
            public string lastName { get; set; }
    
            public virtual Address Address { get; set; }
    
            public List<Associate> Associates { get; set; }
            public List<Address> Addresses { get; set; }
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    
    namespace CompositeAppointment.Domain.Entities
    {
        public class Address
        {
            [Key]
            [Column("Address_ID")]
            public int AddressID { get; set; }
    
            [Column("CompPerson_ID")]
            public int PersonID { get; set; }
    
            public virtual Subject Subject { get; set; }
    
            [Column("FName")]
            public string FirstName { get; set; }
    
            [Column("LName")]
            public string LastName { get; set; }
    
            [Column("Address1")]
            public string Address1 { get; set; }
    
            [Column("Address2")]
            public string Address2 { get; set; }
    
            [Column("City")]
            public string City { get; set; }
    
            [Column("USStateCode")]
            public string State { get; set; }
    
            [Column("PostalCode")]
            public string Zip { get; set; }
    
            [Column("Country")]
            public string Country { get; set; }
    
            [Column("PhoneVoice")]
            public string Phone { get; set; }
    
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using CompositeAppointment.Domain.Entities;
    
    namespace CompositeAppointment.Domain.Abstract
    {
        public interface ISubjectRepository
        {
            
            Subject GetSubject(string userName, string passWord);
    
            void SaveSubject(Subject subject);
    
          }
    }
    
     

    WhiskeyRomeoLima
    Thursday, October 20, 2011 4:22 PM

Answers

All replies

  • Hi WhiskeyRomeoLima,

    Try your query like this:

    return context.Subjects.Include("Addresses").Where(p => p.Email == userName && p.webPassword == passWord).Single();
    

     


    Tom Overton
    Thursday, October 20, 2011 5:10 PM
  • Hi Tom,
    Thanks for the reply.  That compiles and run but does not actually load the address in Addresses property of Subject.  The little code block below proved that.  I also noticed the .Include was expecting a string but every example of using this in every tutorial concerning loading related data shows a lambda expression instead.  Also my context must not be correct if intellisense does not recognize a => a.Addresses in the Include method.
     
                        Subject subject = repository.GetSubject(model.UserName, model.Password);
                        foreach (Address a in subject.Addresses)
                        {
                            string s = a.Address1;
                        }
    

    WhiskeyRomeoLima
    Thursday, October 20, 2011 5:54 PM
  • Hi WhiskeyRomeoLima,
    Welcome!
    According to  your code, the default Lazy Loading has been turned off by this code:
       public List<Associate> Associates { get; set; }
            public List<Address> Addresses { get; set; }
    
    

    you can try to the following ways:
    one way: add "virtual" before the collection-->public virtual List<Associate> Associates { get; set; }--->use Include("navigation name")
    another way: Explictly loading---> context.Entry(subject).Reference(u => u.Addresses).Load();
    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 21, 2011 3:07 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 1:59 AM
    Moderator
  • That line of code produces this error:

     "The property 'Addresses' on type 'Subject' is a collection navigation property. The Collection method should be used instead of the Reference method."

    The reality here is I am not going to learn EF here in this forum,  so I am going back to square one and use the Lerhman book.  This will significantly delay my project but I feel that is only option I have.


    WhiskeyRomeoLima
    Monday, October 24, 2011 3:38 PM
  • Hi,

    Sorry for my careless, we should try "Collection" method:

     context.Entry(subject).Collection(u => u.Addresses).Load();

    I'm not sure if it works for you.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 27, 2011 7:45 AM
    Moderator
  • Alan,

    Thanks for the reply.  Check this post I made concerning Afluent API  which ended solving this problem.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/be347961-5ce8-47f8-a149-f4e9afccf341

    Bill


    WhiskeyRomeoLima
    Thursday, October 27, 2011 4:21 PM