[C#] Use of Mapster object mapping

Series of articles

[C#] Number generator (define order number rules, fixed characters, serial numbers, business order numbers)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129129787

[C#] Date range generator (start date, end date)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129040663

[C#] Component development, calling dll component methods
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129492112

[C#] Use of data entity classes
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/128816638

[C#] Document approval flow plan
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/128972545

【C#】Barcode Management Operation Manual
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126589496

[C#] WebAPI publishing and exception handling under IIS platform
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126539836

[C#] Code template generation tool
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126890673

[C#] MySQL database import tool (batch Excel insertion)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126427323

[C#] Simple QR code production and printing tool
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/126884228

[C#] The most complete document printing (printing template, barcode & QR code, font style, project source code)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129415723

[C#] Windows service (Service) installation and start-stop solution
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/124053794

[C#] Penetrate Session isolation, service calls external programs (no form interface solution)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/124053033

[C#] Task planning implementation, using the Quartz class
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/123667723

[C#] Source code parsing regular expression
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/118337074

[C#] Software version and file MD5 record (XML operation)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/112513871

[C#] Test whether the network is connected
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/110137288

[C#] Get the code based on the name (Dictionary get key method)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129816701

[C#] Data modeling, should you use DataTable or List?
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129792726

[C#] Two-way binding between GridControl control and data set
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/129423755

[C#] GridControl dynamically replaces DataSource and handles data query exceptions
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130305424

[C#] GridControl date field displays hours, minutes and seconds
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130718303

[C#]GridControl adds a selection column (no need to click twice)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/130763713

[C#] Database checking tool (can be accessed across databases)
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/97172329

[C#] Code analysis – printing data set
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/131431829

[C#] Code analysis – intercept the entire method function
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/109817809

[C#] Reflection mechanism, dynamic loading of class files
Link to this article: https://blog.csdn.net/youcheng_ge/article/details/131435110

Article directory

  • Series of articles
  • Preface
  • 1. Problem description
  • 2. Solution
  • 3. Software development (source code)
    • 3.1 Introduction of development package
    • 3.2 Database entity class Entity
    • 3.3 Data transfer object Dto
    • 3.4 Dto to Entity mapping
  • 4. Project display
  • 5. Resource links

Foreword

I can abstract the whole world, but I can’t abstract you. Want to make you a private constant so external functions cannot access you. I also want you to become a global constant so that I can call you throughout my life cycle. It’s a pity that there is no such constant in the world, and I can’t define you, because you are so specific in my heart.

Hello everyone, this column is the [Project Practice] column. It is different from the [Underlying Library] column. We can find that the “Problem Description” and “Project Display” chapters have been added, which is very consistent with the project development process and allows readers to have a clearer understanding of the project. The problems that are solved and the effects that the product can achieve. This column contains solutions to the project development process, which is a refinement of my relatively mature and reliable project development methods. I have sorted out the solutions to these problems and written this article to share them with you. If you encounter similar problems, you can deal with them according to the solutions in this article.

This column will continue to be updated and improved, and the column articles are weakly related (the dependence between articles is weak and there is no reading order). If you have any questions, you can send me a private message. If you are interested in this column, please follow it. I will help you use the simplest code to implement complex functions.

·Tips: This column is a practical project article. Students who have not been exposed to project development may have difficulty understanding it and it is not recommended to read it.
3A0N000001

1. Problem description

Use of Mapster object mapping

?

2. Solution

3. Software development (source code)

3.1 Introduction of development package

Mapster

3.2 Database entity class Entity

DBFirst automatically generates database entities based on database tables, or you can use my tool to generate class files.

//--------------------------------------------- ----------------------------------
// Entity entity class, also called DBModel.
// This code is automatically generated by the tool, please do not create it manually.
// If you regenerate the code, manually adding content will be overwritten.
//------------------------------------------------ ----------------------------------

using System.ComponentModel.DataAnnotations;
using System;
using System.Linq;
using System.Text;
using SqlSugar;


namespace YS.Model
{<!-- -->
    ///<summary>
    ///basic information
    ///</summary>
    [SugarTable("Bare Sand Raw Material Information Table")]
    public class bare sand raw material information table
    {<!-- -->
        public bare sand raw material information table()
        {<!-- -->
        }

        /// <summary>
        /// describe :  
        /// Empty value: False
        /// default : 
        /// </summary>
        [Display(Name = "")]
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public long AutoID {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string company number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: four issues
        /// </summary>
        [Display(Name = "")]
        public string factory name {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string inventory code {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string purchase order number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string application form number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string sales order number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string supplier number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string bare sand raw material type {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string bare sand raw material number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string sand specification {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string sand model {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string status description {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 0.000
        /// </summary>
        [Display(Name = "")]
        public decimal? current quantity {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Marking time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Marking entry time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string marking person number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 0.000
        /// </summary>
        [Display(Name = "")]
        public decimal? Marking quantity {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string marking remarks {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        /// Default: No
        /// </summary>
        [Display(Name = "")]
        public string whether to isolate {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Isolation time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Isolated entry time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string isolation person number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string isolation reason {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        /// Default: No
        /// </summary>
        [Display(Name = "")]
        public string whether to release {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Release time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Release entry time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string releaser number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string release reason {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Inspection passing time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string inspection qualified sample number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 0.000
        /// </summary>
        [Display(Name = "")]
        public decimal? Inspection qualified quantity {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? First storage time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: production use
        /// </summary>
        [Display(Name = "")]
        public string receiving type {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Receipt time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Get the entry time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string recipient number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string remark {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Retirement time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Return entry time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string withdrawal operator number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string reason for withdrawal {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string withdrawal remarks {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Purchase return time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Purchase return entry time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string purchase return operator number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string purchase return reason {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string purchase return remarks {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? scrap time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: 1000-01-01 00:00:00
        /// </summary>
        [Display(Name = "")]
        public DateTime? Scrap entry time {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string Scrap operator number {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string scrapping reason {<!-- --> get; set; }

        /// <summary>
        /// describe :  
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string scrap remarks {<!-- --> get; set; }

        /// <summary>
        /// Description: Warehouse code
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string warehouse encoding {<!-- --> get; set; }

        /// <summary>
        /// Description: Cargo location code
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "")]
        public string cargo location code {<!-- --> get; set; }

    }
}

3.3 Data transfer object Dto

Create a data transfer object, which is the data entity that the front end transmits to the backend, which is customized by the user.
In my front-end drop-down selection here, there is a key value, which needs to be passed to the backend to construct the number field. However, the database storage is the stored name, so there is no need to refer to it for display.

Integrated from the entity column of 3.2, and slightly added a few fields on this basis.

//--------------------------------------------- ----------------------------------
// Entity entity class, also called DBModel.
// This code is automatically generated by the tool, please do not create it manually.
// If you regenerate the code, manually adding content will be overwritten.
//------------------------------------------------ ----------------------------------

using System.ComponentModel.DataAnnotations;
using System;
using System.Linq;
using System.Text;
using SqlSugar;


namespace YS.Model
{<!-- -->
    ///<summary>
    ///basic information
    ///</summary>
    [SugarTable("Bare Sand Raw Material Information Table")]
    public class bare sand raw material information tableDto: bare sand raw material information table
    {<!-- -->

        /// <summary>
        /// Description: Bare sand raw material type code
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "Bare sand raw material type code")]
        public string bare sand raw material type code {<!-- --> get; set; }

        /// <summary>
        /// Description: Sand specification code
        /// Null value: True
        ///Default: sand specification code
        /// </summary>
        [Display(Name = "Sand Specification Code")]
        public string sand specification code {<!-- --> get; set; }

        /// <summary>
        /// Description: Sand model code
        /// Null value: True
        ///Default: empty
        /// </summary>
        [Display(Name = "Sand Model Code")]
        public string sand model code {<!-- --> get; set; }

    }
}

3.4 Dto to Entity mapping

Introduce namespace

using Mapster;

Entity mapping
Adapt mapping keyword

//Convert DataTable to object processing
var sqlSugar1 = new BaseService<bare sand raw material information table Dto>();
List<Bare Sand Raw Material Information Table Dto> list1 = sqlSugar1.Db.Utilities.DataTableToList<Bare Sand Raw Material Information Table Dto>(Dt_Info);

// list is just one piece of data
Bare sand raw material information table Dto Bare sand raw material information table Dto_Model = list1[0];

string str_LastAutoCode = AutoCode(string.Format("Y{0}{1}0{2}{3}[3-digit serial number]",
     Bare sand raw material information table Dto_Model.Supplier number,
     Bare sand raw material information table Dto_Model. Bare sand raw material type code,
     Bare sand raw material information table Dto_Model. Sand specification code,
     Bare sand raw material information table Dto_Model. Sand model code
     ),
     "Bare Sand Raw Material Information Table", "Bare Sand Raw Material Number");

Bare sand raw material information table Dto_Model. Bare sand raw material number = str_LastAutoCode;
Bare sand raw material information table Dto_Model. Status description = Bare sand raw material status. Waiting for bare sand raw material sample inspection;
Bare sand raw material information table Dto_Model.Marking time = DateTimeNow;
Bare sand raw material information table Dto_Model.Marking entry time = DateTimeNow;
Bare sand raw material information table Dto_Model. Marker number = UserID;


//Do table mapping, Dto->Entity
var info = bare sand raw material information table Dto_Model.Adapt<bare sand raw material information table>();
var sqlSugar2 = new BaseService<bare sand raw material information table>();
sqlSugar2.Add(info);

Some people may ask why there is DataTableToList() processing, but I can’t help it. The parameters in the interface have to be defined as DataTable. I think it is best to transfer the object and directly define it as a generic type. They don’t listen to me. of.
Therefore, I converted the data background processing and used list processing.

4. Project display

slightly

5. Resource links

slightly