【问题标题】:Dapper select inner join mapping to a modelDapper 选择内部连接映射到模型
【发布时间】:2021-06-14 01:52:43
【问题描述】:

假设我有以下表格

  Table Products
  --------
  Id (int)
  Name (varchar)
  BrandId (int)

  Table Brands
  -----------
  Id (int)
  BrandName (varchar)

以及 CSharp 中的以下类

public class Product{
   public int Id { get; set;}
   public string Name { get;}
   public ProductBrand Brand { get;}
}
public class ProductBrand{
   public int Id { get; set;}
   public string BrandName { get;}
}

我如何Select inner join 这两个表并映射到 ProductBrand 模型及其所有属性在 dapper 中?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc dapper


    【解决方案1】:

    您可以使用如下查询方法:

    using (IDbConnection connection = new SqlConnection(ConnectionString))
    {
        string sql = "SELECT Products.Id, BrandName FROM Products INNER JOIN Brands ON Products.BrandId = Brands.Id";
        var result = connection.Query<ProductBrand>(sql);
    }
    

    【讨论】:

    • 我想获得一个产品模型。这给了我一个 ProductBand 模型
    【解决方案2】:

    工作代码,请进行如下更改,

        public class Product{
       public int Id { get; set;}
       public string ProductName { get;}
    }
    public class Brand{
       public int BrandId { get; set;}
       public string BrandName { get;}
       public IList<Product>Products { get;}
    }
    
    var sql ="SELECT Id, ProductName, BrandId, BrandName FROM Brand B INNER JOIN Product  P ON B.BrandId=P.BrandId"
    var products = connection.QueryAsync<Brand, Product, Brand>(sql, 
    (brand, product) => {
    if(brand.Products==null)
    {
        brand.Products = new List<Product>;
    }
        brand.Products.Add(product);
        return brand;
    },
    splitOn: "BrandId");
    

    【讨论】:

      【解决方案3】:

      如果你想通过创建过程来使用它

      CREATE PROCEDURE dbo.GetProducts
      
      AS
      BEGIN
          SELECT 
              Products.Id,
              Brands.BrandName
          FROM Products
              INNER JOIN
              Brands ON Products.BrandId  = Brands.Id
      
      END
      GO
      
      using (IDbConnection db = new SqlConnection("ConnectionString"))
      {
          
         var productBrands = db.Query<ProductBrand>("GetProducts").ToList();
          
      }
      

      【讨论】:

        【解决方案4】:

        Dapper 中的关系管理则相反——每个品牌都应该有一个产品列表。

        所以类应该是这样的:

        public class Product
        {
            public int Id { get; set;}
            public string Name { get;}
        }
        
        public class Brand
        {
            public int Id { get; set;}
            public string BrandName { get; set; }
            public ICollection<Product> Products { get; set; }
        }
        

        然后你可以这样查询:

        var products = connection.QueryAsync<Product, Brand, Product>(sql, 
        (product, brand) => {
            product.Brand = brand;
            return product;
        },
        splitOn: "BrandId");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-13
          • 1970-01-01
          • 1970-01-01
          • 2021-02-03
          • 2019-07-30
          • 1970-01-01
          • 2023-02-01
          • 1970-01-01
          相关资源
          最近更新 更多