如何使用实体框架.NET插入Postgres-模型问题
我是 .net 和实体的新手,我认为我有一个非常基本的问题让我头疼。我正在尝试使用实体框架为 Postgres DB 构建一个简单的 CRUD 控制器。 设置已完成,我可以使用我的 API 通过 Get 获取 SELECT。
现在我正在尝试使用 PUT 来插入。我有点困惑,似乎没有找到答案。我有一个现有的数据库并使用实体脚手架方法创建了数据模型。 当我尝试通过 POST 获取模型数据时,出现以下错误:
{“错误”:{ “ID”: [ “无法找到用于 User 类型的构造函数。类应该有一个默认构造函数,一个带有 参数或用 JsonConstructor 属性标记的构造函数。 路径“id”,第 2 行,位置 7。” ], “用户”:[ “用户字段为必填项。” ] }, ...
我不明白如何配置构造函数?
这是我的控制器代码:
[HttpPost("user")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> CreateUser(User user)
{
_dbcontext.Add(user);
await _dbcontext.SaveChangesAsync();
var allUsers = _dbcontext.Users.ToList();
return Ok(allUsers);
}
这是我的模型:
using System;
using System.Collections.Generic;
namespace data_services_api.Models
{
public class User
{
public User()
{
Companies = new HashSet<Company>();
Employees = new HashSet<Employee>();
Profiles = new HashSet<Profile>();
}
public int Id { get; set; }
public DateTime Created { get; set; }
public string Email { get; set; } = null!;
public string? Firstname { get; set; }
public string? Lastname { get; set; }
public virtual ICollection<Company> Companies { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
public virtual ICollection<Profile> Profiles { get; set; }
}
}
我将感谢您的帮助。谢谢
i am newby in .net and entity and i think i have a very basic problem what gives me headache. I am trying to build a simple crud controller for a Postgres DB using entity framework.
The setup is complete and i can get SELECTs via Get using my API.
Now I am trying to use a PUT to INSERT. I am a little confused and don't seem to find the answer. I have a existing Database and created the Datamodel usings Entitys scaffold method.
When i try to get my Model data via the POST it gives following error:
{ "errors": {
"id": [
"Unable to find a constructor to use for type User. A class should either have a default constructor, one constructor with
arguments or a constructor marked with the JsonConstructor attribute.
Path 'id', line 2, position 7."
],
"user": [
"The user field is required."
] }, ...
I don't get how i configure the constructor?
Here is my Controller code:
[HttpPost("user")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> CreateUser(User user)
{
_dbcontext.Add(user);
await _dbcontext.SaveChangesAsync();
var allUsers = _dbcontext.Users.ToList();
return Ok(allUsers);
}
And here is my Model:
using System;
using System.Collections.Generic;
namespace data_services_api.Models
{
public class User
{
public User()
{
Companies = new HashSet<Company>();
Employees = new HashSet<Employee>();
Profiles = new HashSet<Profile>();
}
public int Id { get; set; }
public DateTime Created { get; set; }
public string Email { get; set; } = null!;
public string? Firstname { get; set; }
public string? Lastname { get; set; }
public virtual ICollection<Company> Companies { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
public virtual ICollection<Profile> Profiles { get; set; }
}
}
I would appreciate your help. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论