asp.net - 如何将 JObject 作为对象添加到实体框架
问题描述
我有一个代表反序列化 jsonjobject
的变量列表,我想将它们添加到实体数据库中。我怎样才能做到这一点?
到目前为止尝试的解决方案:
var obj = JsonConvert.DeserializeObject<dynamic>(strtest);
foreach (var item in obj.results)
{
db.ReedAPIModels.Add(item);
db.SaveChanges();
}
模型类代表数据库:
public class APIModel
{
[Key]
public int jobId { get; set; }
public int employerId { get; set; }
public string employerName { get; set; }
public string employerProfileId { get; set; }
public string employerProfileName { get; set; }
public string jobTitle { get; set; }
public string locationName { get; set; }
public int minimumSalary { get; set; }
public int maximumSalary { get; set; }
public string currency { get; set; }
public string expirationDate { get; set; }
public string date { get; set; }
public string jobDescription { get; set; }
public int applications { get; set; }
public string jobUrl { get; set; }
}
API 结果
{
"results": [
{
"jobId": 39650785,
"employerId": 375315,
"employerName": "Spectrum IT Recruitment (South)",
"employerProfileId": null,
"employerProfileName": null,
"jobTitle": "Senior Front End Developer",
"locationName": "Reading",
"minimumSalary": 60000,
"maximumSalary": 70000,
"currency": "GBP",
"expirationDate": "17/01/2020",
"date": "03/01/2020",
"jobDescription": " Senior Front End Developer - Reading, Berkshire Senior Front End Developer required by a growing company based in Reading, Berkshire. The company are looking to recruit a Senior Front End Developer to join their expanding development team to work on the full design and development lifecycle of new products. The successful Senior Front End Developer will have lots of opportunities for development of both techni... ",
"applications": 0,
"jobUrl": "https://www.reed.co.uk/jobs/senior-front-end-developer/39650785"
}
],
"ambiguousLocations": [],
"totalResults": 9007
}
另外,我尝试过使用ToObject
方法
解决方案
您应该添加APIModel
而不是ReedAPIModels
.
你可以试试这种方式
var apiResult = JsonConvert.DeserializeObject<dynamic>(strtest);
foreach (var item in apiResult.results)
{
db.APIModel.Add(item);
db.SaveChanges();
}
推荐阅读
- django - 使用来自其他模型的对象填充 django 模型
- android - 如何让透明容器从背景中脱颖而出?
- qt - Qt Creator qt.qpa.plugin:无法在 Ubuntu 20.04 上加载 Qt 平台插件“xcb”
- javascript - 我的动态网站没有响应特定的按钮点击
- c++ - 使用 WIndows 10、Visual Studio 2019 运行 MongoDB 的基本 C++ 代码时出错
- javascript - 如何在没有用户交互的情况下捕获标签关闭事件
- html - jQuery 和 img 不起作用
- domain-driven-design - 使用 DDD 进行设计时,我应该将文章类别建模为值对象吗?
- html - 如何调整 SVG 的位置?
- python - 等效于 Linux for Popen 上的 creationflags=CREATE_NEW_CONSOLE