2011-08-26 16 views
0

我想創建一個特定的類來管理我的應用程序中的大學生。如何在EF中爲集合創建特定的類?

例如,我有一個商店,我有一個集合中的客戶列表,在這個集合中,我有一個客戶是本月的客戶,有些客戶獲得了一些折扣原因。讓我們來看看代碼:

public class Store { 
    public ICollection<Customer> Customers { get; set; } 

    public Customer CustomerOfTheMonth 
    { 
     //get and set for customer of the month 
    } 
    public ICollection<Customer> DiscountCustomers 
    { 
     //get and set for customer of the month 
    } 
    public ICollection<Customer> GetAllCustomers 
    { 
     //get and set for customer of the month 
    } 
} 

但是在我的數據庫中,我只有兩個表。商店和客戶。

我想要做的是爲客戶創建一個特定的集合,從Store中刪除邏輯並放入特定的類,畢竟我不覺得這些邏輯屬於這兩個類。

我廣域網tomething這樣的:

public class Store { 
    internal CustomerCollection Customers { get; set; } 

    //get and set for the propertis, just delegating for the collection 
}   

public class CustomerCollection { 
    public ICollection<Customer> Customers { get; set; } 

    public ICollection<Customer> DiscountCustomers 
    { 
     //get and set for customer of the month 
    } 

    //get and set with logic to filter the collection 
} 

有沒有去創建這個映射,並保持只有兩個數據庫中的表?我想讓它對應用程序透明。對於代碼問題抱歉,鍵入堆棧溢出並沒有檢查語法。

回答

2

不需要爲您的模型類創建業務邏輯。將你的邏輯分爲上層。這裏是你的模型類。這將創建你的關係,只要你想

public class Store { 
     public vertual ICollection<Customer> Customers { get; set; } 

     //get and set for other propertis 
    } 



public class Customer{ 

    //get and set for other propertis 
} 

創建一個存儲庫或服務層,應用特定的業務邏輯,

如果要加載商店的客戶一次就可以使用預先加載
public ICollection<Customer> GetDiscountCustomers() 
    { 
     return dbContext.Customers.where(c=>c.discount=true).ToList() 
    } 

public ICollection<Store> GetAllStores() 
     { 
      return dbContext.Stores.Include("Customers").ToList() 
     } 
+0

當我加載存儲時,客戶被加載在一起,我不想分開它。 – Migore

+0

然後你可以使用急切的加載。 –