自在猫先生 2023-04-21 10:09 采纳率: 62.9%
浏览 47
已结题

.NET CORE 5.0 使用三层架构,实现根据传递的数据新建数据表

我搭建了.net core 的三层架构项目,定义了一个接口接收postman传递的json数据实现新建数据表的功能
实体层:

public class FWTB
    {
        [Key]
        public int Id { get; set; }
        public class Database
        {
            [Key]
            public int Id { get; set; }
            public string Databases { get; set; }
        }
        public class Constraints
        {
            [Key]
            public int Id { get; set; }
            public string Relation { get; set; }
        }
        public class Structure
        {
            [Key]
            public int Id { get; set; }
            public string StoredProcedures { get; set; }
            public string View { get; set; }
           
            public string Index { get; set; }
            public string PrimayKey { get; set; }
            public string Triggers { get; set; }
        }
        public class Tables
        {
            [Key]
            public int Id { get; set; }
           
            public string Table { get; set; }
            public Constraints Constraints { get; set; }
            public Structure Structure { get; set; }
        }
        public class Db
        {
            [Key]
            public int Id { get; set; }
            public Database Databse { get; set; }
            public List<Tables> Tables { get; set; }
        }

        public Db db { get; set; }




    }

控制器层:

public async Task<IActionResult> AddTableInvent([FromBody] FWTB fWTB)
        {
            // 将 JObject 转换为实体对象
           // var fWTBDto = JsonConvert.DeserializeObject<FWTBDto>(data.ToString());
            await _tableInventService.AddTableInvent(fWTB);
            return Ok();
        } 
json数据:

{
 "Id":1,
      "db": {
       "Id": 2,
        "databse": {
           "Id": 3,
            "databases": "fwt-db"
        },
        "tables": [
            {
                "Id": 4,
                "table": "数据表1:fwt-tb00",
                "constraints": {
                   "Id": 5,
                    "Relation": "关系:主外键"
                },
                "structure": {
                   "Id": 6,
                    "storedProcedures": "存储过程:pro-fwt-tb00",
                    "view": "FWT_BY",
                    "Index": "索引:priindex",
                    "PrimayKey": "主键:id",
                    "triggers": "trigger_FWT"
                }
            }
        ]
    }
}
实现服务层:
public async Task AddTableInvent(FWTB fWTB)
        {
            await _tableInventRepository.AddTableInvent(fWTB);
        }
存储层:
public async Task AddTableInvent(FWTB fWTB)
        {
            await _mydbContext.FWTB.AddAsync(fWTB);
            await _mydbContext.SaveChangesAsync();
        }
我发送请求的时候提示:
System.InvalidOperationException: Unable to track an instance of type 'FWTB' because it does not have a primary key. Only entity types with primary keys may be tracked.
说我没有定义主键值,但是我明明定义了主键。
  • 写回答

3条回答 默认 最新

  • threenewbee 2023-04-21 11:04
    关注

    需要跟踪的表,必须有主键,道理很简单,否则怎么知道哪个记录是哪个

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月26日
  • 创建了问题 4月21日

悬赏问题

  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 在rhel8中安装qemu-kvm时遇到“cannot initialize crypto:unable to initialize gcrypt“报错”
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 paddle库安装时报错提示需要安装common、dual等库,安装了上面的库以后还是显示报错未安装,要怎么办呀?
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀