处理不直接映射到数据库列的属性

发布于 2025-01-03 08:26:58 字数 2055 浏览 4 评论 0原文

我有下表

客户端表 和 产品表

ID
Name

ClientProduct 表

ID
ClientID
ProductID

产品类

 private int id;
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
     protected string name;

    public Product () { }

    public Product (string name)
    {
        this.name = name;
    }

    public Product (string name)
    {
        this.name = name;
    }   
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

客户端类

     private int id;
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
     protected string name;

    public Client () { }

    public Client (string name)
    {
        this.name = name;
    }

    public Client (string name)
    {
        this.name = name;
    }   
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

ClientProduct 类

        protected Client client;
    protected Product product;

    public ClientProduct  () { }

    public ClientProduct  (Client client,  Product product)
    {
        this.client= client;
        this.product= product;
    }

    public Client client        {
        get { return client; }
        set { client= value; }
    }

    public Product product      {
        get { return product; }
        set { product= value; }
    }

我如何在 petaPOCO 中执行以下操作?

    public static System.Collections.Generic.IList<ClientProduct> LoadForClient(Client client)
    {
        if (null != client)
            return Load("ClientID = " + client.ID);
        else
            return null;
    }

这样我就可以获得该客户的所有产品的列表,稍后我将在我的视图中使用这些产品

 private void LoadProducts(Client client )
    {
        Products = ClientProduct.LoadForClient(client)
            .Select(x => x.Product.Name)
            .OrderBy(x => x).ToArray();
    }

I have the following tables

Client Table and Product Table

ID
Name

ClientProduct Table

ID
ClientID
ProductID

Product class

 private int id;
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
     protected string name;

    public Product () { }

    public Product (string name)
    {
        this.name = name;
    }

    public Product (string name)
    {
        this.name = name;
    }   
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

Client class

     private int id;
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
     protected string name;

    public Client () { }

    public Client (string name)
    {
        this.name = name;
    }

    public Client (string name)
    {
        this.name = name;
    }   
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

ClientProduct class

        protected Client client;
    protected Product product;

    public ClientProduct  () { }

    public ClientProduct  (Client client,  Product product)
    {
        this.client= client;
        this.product= product;
    }

    public Client client        {
        get { return client; }
        set { client= value; }
    }

    public Product product      {
        get { return product; }
        set { product= value; }
    }

How can I do the following in petaPOCO?

    public static System.Collections.Generic.IList<ClientProduct> LoadForClient(Client client)
    {
        if (null != client)
            return Load("ClientID = " + client.ID);
        else
            return null;
    }

such that I can have list of all products for that client that I will later used in my view as

 private void LoadProducts(Client client )
    {
        Products = ClientProduct.LoadForClient(client)
            .Select(x => x.Product.Name)
            .OrderBy(x => x).ToArray();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

愁杀 2025-01-10 08:26:58

1:M 和 M:1 关系似乎就是您所追求的
http://www .toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships

您可以定义自定义关系器 打回来; Brad 的两个表示例(如果您的产品直接映射到客户端)看起来像这样:

var posts = db.Fetch<Product, Client, ClientProduct>(
    (p,c)=> { p.client_obj = c; return p; },
    @"SELECT * FROM Product
    LEFT JOIN Client ON Product.clientId = Client.id ORDER BY Product.clientId 
    ");

我意识到您处理的是 M:M 关系,因此您需要更新上面的内容以映射三个对象,但概念是相同。关键是调用中的第三个参数 (ClientProduct) 代表连接的行,然后您可以直接从单个列表引用客户和/或产品。

The 1:M and M:1 relationships seem like what you're after
http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships

You can define a custom relator callback; Brad's example for two tables (if your products mapped directly to the client) would look something like this:

var posts = db.Fetch<Product, Client, ClientProduct>(
    (p,c)=> { p.client_obj = c; return p; },
    @"SELECT * FROM Product
    LEFT JOIN Client ON Product.clientId = Client.id ORDER BY Product.clientId 
    ");

I realize your dealing with a M:M relationship so you would need to update the above to map across the three objects, but the concept is the same. The key is that your 3rd argument in the call (ClientProduct) represents the joined row, and you can then reference the Client and/or Products directly from the single list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文