2011-08-21 206 views
1

我有兩個表用戶和公司:實體框架 - 代碼第一個關係:一對一個

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; } 
} 

的配置是這樣...

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); 
    } 
} 

如果我創建一個視圖公司向各公司展示每家公司並在公司中列出一列作爲用戶人數,則視圖按預期呈現,顯示每家公司中的用戶數量。但是,當我嘗試在視圖中顯示每個用戶並在列中顯示Company.Name時,則表示Company爲null。有人可以解釋我的一對一關係是否在用戶和公司之間搞砸了嗎?

** * ** * ** * ** *編輯* ** * ** * ** * ** * ***

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); 
    } 

這裏是ApplicationAdministrator類

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; } 
} 

最後錯誤中指定

模式是無效的。錯誤:(144,6):錯誤0019:該 EntitySet的 'UserApplication' 與模式 'DBO' 和表 'ApplicationAdministrators' 已經定義。每個實體集必須 引用一個唯一的模式和表。公用IQueryable用戶 第16行:{返回context.Users.Include(「AdministratorApplications」)。Include(「Company」); } 第18行:} 第19行:

回答

3

你需要讓Company財產virtual

public class User 
{ 
    // Properties 

    public virtual Company Company { get; set; } 

} 

如果你不想讓它virtual你需要告訴EF加載使用Company財產Include方法。

通過使財產virtual EF將延遲加載屬性。但F你正在訪問Company屬性,當你訪問user對象,那麼你可以使用Include方法急於負載Company財產。

var users = context.Users.Include(user => user.Company).Where(/*conditions*/); 
+0

您不能在Include中使用lambda表達式。用虛擬修飾公司屬性引發這個錯誤......「已經有一個與此命令關聯的開放式DataReader,它必須先關閉。」 – bdparrish

+0

@bdparrish你可以發佈你的LINQ查詢以及你如何訪問返回的結果嗎? – Eranga

+0

現在明白了,只需將MultipleActiveResultSets = true屬性添加到我的連接中,這是我以前從未做過的。 – bdparrish

相關問題