2012-03-09 85 views
0

相關項目我有兩個對象,如:的EntityFramework獲得通過ID或現場

public class Area : IEntity 
    { 
     public virtual int Id { get; set; } 
     public virtual int AreaTypeLookupId { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(100)] 
     public virtual string Name { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(2)] 
     public virtual string VMRegionCode { get; set; } 
     public virtual int GISPrimaryKeyId { get; set; } 
     public virtual string ConcatenatedGISAreaIds { get; set; } 
     public virtual int? ParentAreaId { get; set; } 
     public virtual int? ParentRegionAreaId { get; set; } 
     [Column(TypeName = "char")] 
     [StringLength(3)] 
     public virtual string CostCenterCode { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(64)] 
     public string CreatedByUserName { get; set; } 
     public DateTime CreatedDateTime { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(64)] 
     public string LastModifiedByUserName { get; set; } 
     public DateTime? LastModifiedDateTime { get; set; } 

     // Navigation properties 
     public virtual AreaTypeLookup AreaTypeLookup { get; set; } 
    } 

public class AreaTypeLookup : IEntity 
    { 
     [Column("Id")] 
     public virtual int AreaTypeLookupId { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(100)] 
     public virtual string Name { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(10)] 
     public virtual string ShortName { get; set; } 
     public virtual int? AreaTypeLevelLookupId { get; set; } 
     public virtual int? OperationTypeLookupId { get; set; } 
     public virtual int? ParentAreaTypeLookupId { get; set; } 
     public bool IsAvailableToSetBudgetAgainst { get; set; } 
     public int DisplaySortOrder { get; set; } 
     public bool IsProtected { get; set; } 
     public DateTime? DeletedDateTime { get; set; } 
     public int? ManagedByServiceTypeLookupId { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(64)] 
     public string CreatedByUserName { get; set; } 
     public DateTime CreatedDateTime { get; set; } 
     [Column(TypeName = "varchar")] 
     [StringLength(64)] 
     public string LastModifiedByUserName { get; set; } 
     public DateTime? LastModifiedDateTime { get; set; } 
    } 

我想要的是讓地方areatypelookupid = 3。對於這個區域鍵入名稱爲「區域」的所有領域

什麼是獲得代碼的可維護性方面的最佳方式:

此:

var areas = _unitOfWork.AreaRepository.GetAll().Where(x => x.AreaTypeLookupId == 3); 

或這個:

var areas = _unitOfWork.AreaRepository.GetAll().Where(x => x.AreaTypeLookup.Name == "region"); 

在那裏我猜「區域」應該從一個資源文件真正成爲。

回答

0

通常,您將要使用主鍵,因爲它應該是不可變的。但是,如果您可以保證查找名稱不會更改,則可以容忍按名稱進行搜索。我說可以容忍的原因是因爲在主要關鍵字上運行查詢將在數據庫上更有效率,這是不可變的。