.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 { get; set; }
        }

        public Db db { get; set; }




    }

控制器层:

public async Task AddTableInvent([FromBody] FWTB fWTB)
        {
            // 将 JObject 转换为实体对象
           // var fWTBDto = JsonConvert.DeserializeObject(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.
说我没有定义主键值,但是我明明定义了主键。

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

  • 这篇博客: .NET学习笔记----关于.NET Core那些事(3)【配置文件的读取、json文件的通用解析、读取静态文件】中的 二、通过实体类读取节点 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 定义与配置文件中需要获取的标签结构完全一致的实体类:IConfiguration .Bind() ----反序列化

    要读取的json字符串

     "ConnectionStrings": {
        "WriteConnection": "Server=LAPTOP-JU1DEJP1;Database=ZhaoxiDBSet;Trusted_Connection=True;",
        "ReadConnectionList": [
          "Server1=LAPTOP-JU1DEJP1;Database=ZhaoxiDBSet01;Trusted_Connection=True;",
          "Server2=LAPTOP-JU1DEJP2;Database=ZhaoxiDBSet02;Trusted_Connection=True;",
          "Server3=LAPTOP-JU1DEJP3;Database=ZhaoxiDBSet03;Trusted_Connection=True;"
        ]
      }
    

    定义的实体类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace NET5WebApplication.Utility
    {
        public class ConnectionStringOptions
        {
            public string WriteConnection { get; set; }
            public string[] ReadConnectionList { get; set; }
        } 
    }
    

    读取json

    public class IConfigurationController : Controller
        {
            //声明
            private IConfiguration _configuration;
            //构造函数中注册
            public IConfigurationController(IConfiguration configuration)
            {
                this._configuration = configuration;
            }
    
            public IActionResult Index()
            {
                ConnectionStringOptions options = new ConnectionStringOptions();
                //将字符串转换为实体类
                _configuration.Bind("ConnectionStrings", options);
    
                string writeConnection = options.WriteConnection;
                string one = options.ReadConnectionList[0].ToString();
                string two = options.ReadConnectionList[1].ToString();
                string three = options.ReadConnectionList[2].ToString();
                return View();
            }
        }
    

    在这里插入图片描述


  • 您还可以看一下 张颜源老师的21年新接口自动化测试视频postman教程 零基础接口测试课程中的 企业开发主流轻量级的数据交换格式json讲解小节, 巩固相关知识点

实体框架没有追踪到主键,应该是因为你在这个实体类里面定义了太多名为Id的主键了。你把里面的主键改一下名字应该能够解决