如何在 EF 中为集合创建特定的类?

发布于 2024-12-01 18:19:36 字数 1205 浏览 2 评论 0原文

我想创建一个特定的类来管理我的应用程序中的学院。

例如,我有一家商店,并且有一个集合中的客户列表,在这个集合中,我有一个客户是本月的客户,还有一些客户因某种原因获得了折扣以获得奖品。让我们来看代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2024-12-08 18:19:36

不需要为模型类创建业务逻辑。将您的逻辑分离到上层。这是您的模型类。它将根据您的需要创建您的关系

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

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

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
}

Create a repository or service layer to apply specific business logic ,

 public ICollection<Customer> GetDiscountCustomers() 
    {
       return dbContext.Customers.where(c=>c.discount=true).ToList()
    }

if you want to load stores with customers at once you can use eager loading,

public ICollection<Store> GetAllStores() 
        {
           return dbContext.Stores.Include("Customers").ToList()
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文