简体   繁体   English

如何在 C# 中实现 dapper 使用的类

[英]How to Implement classes to be used by dapper in C#

这是数据库,点击这里查看

I have just one simple question, it is how to create classes for these tables from the database, these classes are going to be used by dapper, any type of implementation works, weather it be your personnel or industry standard, just enough that a scarcely experienced new developer can perform crud operations with it我只有一个简单的问题,它是如何从数据库中为这些表创建类,这些类将被 dapper 使用,任何类型的实现都有效,不管它是你的人员或行业标准,就足够了经验丰富的新开发人员可以使用它执行 crud 操作

(it is not a question of experimenting because the start line is i do not know how to do these and there is absolutely no source explaining what i need and i have been scouring through the internet, believe it or not for a month, i am thankful for you concern about me just trying it out and searching, but i must respectfully decline no offense intended, i just want to crud, i hope this will not take much of you time (这不是实验的问题,因为起跑线是我不知道如何做这些,而且绝对没有来源解释我需要什么,我一直在互联网上搜索,信不信由你,我是感谢您对我只是尝试和搜索的关注,但我必须恭敬地拒绝无意冒犯,我只是想粗鲁,我希望这不会占用您太多时间

 public class mem                                     (main table to which every table connects)
{
public int Mem_id {get;set;}
public string Mem_name {get;set;}
public string Mem_gndr {get;set;}
public DateTime Mem_dob {get;set;}
//public int adrs_adrs_id {get;set;}
//public int union_union_id {get;set;}
//public int alot_alot_id {get;set;}
}

public class adrs                            (one to one mandatory relationship with mem table)
{
public int adrs_id {get;set;}
public string adrs_col1 {get;set;}
public string adrs_col2 {get;set;}
}


public class alot                        (one to one optional relationship with mem table)
{
public int alot_id {get;set;}
public string alot_no {get;set;}
}

public class union                           (one to many mandatory relationship with mem table)
{
public int union_id {get;set;}
public string union_nm {get;set;}
}



 public class ci                       (one to many optional relationship with mem table)
{
public int ci_id {get;set;}
public string ci_mob {get;set;}
public string ci_eml {get;set;}
//public int mem_Mem_id {get;set;}
}

 public class flat                                  (many to many relationship with mem table)
{
public int flat_id {get;set;}
public string flat_type {get;set;}
}

   public class mem_has_flat                      (this is the bridge class between mem and flat)
  {                                                (many to many relationship bridge)
    //public int mem_Mem_id {get;set;}
    //public int flat_flat_id {get;set;}
  }

) )

I have written the code, properly formatted it, you must have understood where i am having problem with, it is the connecting properties, the commented out ones are the problem with the double forward slash我已经编写了代码,并对其进行了正确格式化,您一定已经了解我遇到问题的地方,它是连接属性,注释掉的是双正斜杠的问题

THIS TOOK ME LOT OF TIME TO WRITE PLEASE HELP, I KNOW BEGGING REMOVES THE QUESTION, BUT I JUST WANT TO SHOW THAT I HAVE DONE MY RESEARCH, EXPLAINED THAT TO THE BEST OF MY ABBILITIES, LET THIS STAY AS IS DO NOT EDIT IT OUT, HUMBLE REQUEST这花了我很多时间来写请帮忙,我知道乞求删除了这个问题,但我只是想表明我已经完成了我的研究,并尽我所能解释了,让它保持原样不要编辑它, 谦虚的要求

i mean no disrespect i am just badly stuck without time我的意思是没有不尊重我只是没有时间被严重卡住

The easiest way to use Dapper is not to try to map one-to-one with the tables.使用 Dapper 最简单的方法是不要尝试 map 与表格一对一。 Instead, build business domain entities that map to your queries.相反,为您的查询构建 map 的业务域实体。 I can't quite understand your schema (is mem a member or is it something about memory).我不太了解您的架构(是mem成员还是与内存有关)。

Instead, consider a classic order system with three tables: Customer, Order and Order_Item.相反,考虑一个具有三个表的经典订单系统:Customer、Order 和 Order_Item。

Customer
    Customer_Id int,
    Customer_Name varchar(50)
    -- etc 

Order 
    Order_Id int,
    Customer_Id int,
    Order_Date DateTime,
    -- etc

Order_Item
    Order_Item_Id int,
    Order_Id int,
    Item_Name varchar (50),
    Item_Description varchar(500),
    Item_Sold_Price decimal(12, 2),
    Item_Quantity int,
    --etc

I left out nullability, index and FK information from that description.我从该描述中省略了可空性、索引和 FK 信息。

Now consider that you want to show your users a list of the last 10 things they bought, showing the date of purchase现在考虑您想向用户显示他们最近购买的 10 件物品的列表,并显示购买日期

SELECT TOP (10)
    o.OrderDate as PurchasedDate,
    oi.Item_Name as Item,
    oi.Item_Description as Description, 
    oi.Item_Quantity as Quantity, 
    oi.Item_Sold_Price as Price
FROM Order_Item oi
INNER JOIN Order o ON o.Order_Id = oi.Order_Id
INNER JOIN Customer c ON c.Customer_Id = o.Customer_Id
WHERE c.Customer_Id = @Customer_Id

With that query I'd prepare a class that looks like通过该查询,我准备了一个看起来像的 class

public class CustomerOrderItems {
    public DateTime PurchasedDate { get; set; }
    public string Item { get; set; }
    public string Description { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

Note that the member names and types match the column names and types of the result set from the query.请注意,成员名称和类型与查询结果集的列名称和类型相匹配。 Now, assuming you have that query in a string variable named query and a customer ID in a variable named customerId , you could get the results with现在,假设您在名为query的字符串变量中有该查询,在名为customerId的变量中有一个客户 ID,您可以使用

var results = sqlConn.Query<CustomerOrderItems>(query, new {Customer_Id = customerId});

After that, results would contain an IEnumerable<CustomerOrderItems> containing up to ten results.之后, results将包含一个IEnumerable<CustomerOrderItems> ,其中最多包含十个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM