1

I have two tables Users and Companies:

public class User
{
    // Properties
    public long Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Username { get; set; }

    public long AgencyId { get; set; }
    public Company Company { get; set; }

    // Custom Propreties
    [ScaffoldColumn(false)]
    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }
}

public class Company
{
    public long Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

The configuration is as so...

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        this.HasKey(x => x.Id);

        this.Property(x => x.Id);
        this.Property(x => x.FirstName).IsRequired();
        this.Property(x => x.LastName).IsRequired();
        this.Property(x => x.Username).IsRequired();
        this.Property(x => x.CompanyId).IsRequired();

        this.HasRequired(user => user.Company).WithMany().HasForeignKey(user => user.CompanyId);
    }
}

public class CompanyConfiguration : EntityTypeConfiguration<Company>
{
    public CompanyConfiguration()
    {
        this.ToTable("Companies");

        this.HasKey(x => x.Id);

        this.Property(x => x.Id);
        this.Property(x => x.Name).IsRequired();

        this.HasMany(company => company.Users).WithRequired().HasForeignKey(user => user.CompanyId);
    }
}

If I create a view with the Companies to show each company and make one column the Count of Users in the Company, then the View is rendered as expected showing the number of Users in each company. However, when I try to show each user in a View and show there Company.Name in a column, then it says that Company is null. Can someone explain if my one-to-one relationship is screwed up between User and Company?

************ EDIT ****************

    public UserConfiguration()
    {
        this.HasKey(x => x.Id);

        this.Property(x => x.Id);
        this.Property(x => x.FirstName).IsRequired();
        this.Property(x => x.LastName).IsRequired();
        this.Property(x => x.Username).IsRequired();
        this.Property(x => x.CompanyId).IsRequired();

        this.HasRequired(user => user.Company).WithMany().HasForeignKey(user => user.CompanyId);
        this.HasMany(user => user.AdministratorApplications)
            .WithMany(application => application.Administrators)
            .Map(map =>
            {
                map.ToTable("ApplicationAdministrators");
                map.MapLeftKey("ApplicationId");
                map.MapRightKey("UserId");
            });
    }

    public ApplicationConfiguration()
    {
        this.HasKey(x => x.Id);

        this.Property(x => x.Name).IsRequired();
        this.Property(x => x.Accronym).IsRequired();
        this.Property(x => x.Description);

        this.HasMany(application => application.Administrators)
            .WithMany(user => user.AdministratorApplications)
            .Map(map =>
            {
                map.ToTable("ApplicationAdministrators");
                map.MapLeftKey("UserId");
                map.MapRightKey("ApplicationId");
            });
    }

    public ApplicationAdministratorConfiguration()
    {
        this.ToTable("ApplicationAdministrators");

        this.HasKey(x => x.Id);

        this.Property(x => x.Id);
        this.Property(x => x.ApplicationId).IsRequired();
        this.Property(x => x.UserId).IsRequired();

        this.HasRequired(appAdmin => appAdmin.Application).WithMany().HasForeignKey(appAdmin => appAdmin.ApplicationId);
        this.HasRequired(appAdmin => appAdmin.User).WithMany().HasForeignKey(appAdmin => appAdmin.UserId);
    }

Here is the ApplicationAdministrator class

public class ApplicationAdministrator
{
    [Column("Id")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [HiddenInput]
    public long Id { get; set; }

    [Display(Name = "Application")]
    public long ApplicationId { get; set; }
    public virtual Application Application { get; set; }

    [Display(Name = "Administrator")]
    public long UserId { get; set; }
    public virtual User User { get; set; }
}

And finally the error

Schema specified is not valid. Errors: (144,6) : error 0019: The EntitySet 'UserApplication' with schema 'dbo' and table 'ApplicationAdministrators' was already defined. Each EntitySet must refer to a unique schema and table.

Line 15: public IQueryable Users Line 16: { Line 17: get { return context.Users.Include("AdministratorApplications").Include("Company"); } Line 18: } Line 19:

bdparrish
  • 3,216
  • 3
  • 37
  • 58

1 Answers1

3

You need to make Company property virtual

public class User
{
    // Properties

    public virtual Company Company { get; set; }

}

If you don't want to make it virtual you need tell EF to load Company property using the Include method.

By making the property virtual EF will lazy load the property. But f you are accessing Company property when you access user object then you can use Include method to eager load Company property.

var users = context.Users.Include(user => user.Company).Where(/*conditions*/);
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • you cannot use a lambda expression with Include. dressing Company attribute with virtual throws this error ... "There is already an open DataReader associated with this Command which must be closed first." – bdparrish Aug 21 '11 at 09:31
  • @bdparrish can you post your LINQ query and how you are accessing the returned results? – Eranga Aug 21 '11 at 09:42
  • got it now, just had to add the MultipleActiveResultSets=true attribute to my connection, which I have never had to do before. – bdparrish Aug 21 '11 at 09:51
  • @bdparrish `MultipleActiveResultSets=true` would resolve the issue but you will run into [Select N+1 problem](http://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem). You probably need aother `Include` statements. – Eranga Aug 21 '11 at 12:11
  • yeah, I had `context.Users.Include("Applications").Include("Company");`.Now I have an issue with saving to the context. It is throwing an error referencing ApplicationAdministrators1 table, but I don't have that table. Would it be creating multiple tables as temp tables because of MARS? – bdparrish Aug 22 '11 at 10:27
  • @bdparrish could be. check your EF mappings. You may have miss configured some mappings. Check whether you have a many-to-many relationship that corresponds to `ApplicationAdministrators` – Eranga Aug 22 '11 at 11:20