2011-08-26 69 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

然后你可以使用急切的加载。 –