如何在 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 中删除并放入特定的类中,毕竟我不觉得该逻辑不属于这两个类。
我想要这样的事情:
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
}
是否可以创建此映射并在数据库中仅保留两个表?我想让它对应用程序透明。抱歉,代码有问题,输入了堆栈溢出,没有检查语法。
I want to create a specific class to manage a collegion in my application.
For example, I have a Store and I have a list of customers in a collection, in this collection I have a customer that is the customer of the month, and some customers that got a prize to get a discount for some reason. Let's get to the code:
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
}
}
But in my database, I only have two tables. Store, and Customer.
What I want to do is create a specific collection for the customer, to remove the logic from the Store and put in a specific class, after all, I don't feel that the logic belongs to neither of those classes.
I wans tomething like this:
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
}
Is there away to create this mapping and keep with only two tables in the database? I want to make it transparent to the application. Sorry for the code problems, typed in stack overflow and didn't check the syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要为模型类创建业务逻辑。将您的逻辑分离到上层。这是您的模型类。它将根据您的需要创建您的关系
创建存储库或服务层以应用特定的业务逻辑,
如果您想立即向商店加载客户,您可以使用预先加载,
don't need to create your business logic to your model classes. Separate your logic to upper level. Here are your model classes. It will create your relationships as you want
Create a repository or service layer to apply specific business logic ,
if you want to load stores with customers at once you can use eager loading,