You need to enable JavaScript to run this app. 如何为C#中供Dapper使用的关联数据库表编写实体类以实现CRUD操作
优惠活动
大模型
产品
解决方案
定价
更多

如何为C#中供Dapper使用的关联数据库表编写实体类以实现CRUD操作

这是数据库结构示意图,点击可查看

我是开发经验较少的入门开发者,目前有明确的技术需求:需要为现有数据库中的多张表设计可在C#项目中供Dapper使用的实体类,要求实现方案无论是个人实践形式还是行业通用标准均可,核心是能帮助入门开发者顺利完成基础CRUD操作。

我针对现有表结构已经初步编写了对应实体类,但目前存在明确的困惑:面对不同类型的表关联关系,不知道如何定义跨表关联的连接属性(也就是代码中被双斜杠注释的关联字段)。我已经花费了一个月时间在网络上查找相关参考资料,但始终没有找到匹配我需求的实现指引,时间较为紧张,希望能获得清晰的设计方案。

以下是我初步编写的实体类代码,各类对应的表关联关系已在代码中标注:

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; }
}

当前我需要解决的核心问题就是上述代码中被注释的关联属性的设计方法,希望能获得可直接落地、适合新手使用的实现方案。


内容的提问来源于stack exchange,提问作者user14889910

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.04 16:36:07