Entity Framework - Code First relationship: one-to-one
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:
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*/);
精彩评论