
DeepSeek+dify 工作流应用,自然语言查询数据库信息并展示
API 本质上是位于数据库之上的一层安全封装,让外部只能通过受控接口访问、修改数据;而数据库就像“智能文件柜”或“高级 Excel”,支持高速检索与多表关联,才被各大企业广泛采用。
API
根目录右键新增 Models 文件夹。在 Models
内新建 Stock.cs
,并依次添加属性:
public class Stock
{
public int Id { get; set; } // 主键 ID
public string Symbol { get; set; } = string.Empty;
public string CompanyName { get; set; } = string.Empty;
[Column(TypeName = "decimal(18,2)")]
public decimal PurchasePrice { get; set; } // 金额需用 decimal,保留两位小数
public decimal LastDividend { get; set; }
public string Industry { get; set; } = string.Empty;
public long MarketCap { get; set; } // 市值可能达到万亿,用 long
}
Stock.Id
;类似“父母—子女”关系:一个 Stock 可能关联多条 Comment,却只能对应一条。
public class Stock
{
// … 上述属性 …
// 一对多:一个 Stock 可有多条评论
public List < Comment > Comments { get; set; } = new();
}
Comments
作为导航属性,允许通过 .Comments
快速访问关联的所有评论。在 Models/Comment.cs
中新增:
public class Comment
{
public int Id { get; set; } // 主键
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public DateTime CreatedOn { get; set; } // 创建时间
// 外键字段:引用 Stock.Id
public int StockId { get; set; } // 外键【截图时间节点:4:05】
// 导航属性:对应单个 Stock(“多”端导航到“父”实体)
public Stock Stock { get; set; } = null!;
}
无需手动使用 Fluent API,EF Core 默认通过以下约定自动识别一对多关系:
父类型
+ Id
的外键字段(如 StockId
)List < Comment > Comments
)原文引自YouTube视频:https://www.youtube.com/watch?v=jMFaAc3sa04