需要朋友字段。 (导航属性字段)
因此,我正在探索ASP.NET Core(WebAPI)。我创建了2个模型,制作了上下文文件,在类 friend 和 videocollection之间添加了一对多关系。现在,当我尝试将创建数据发布到两个表中时,我会收到验证错误,因为我没有从浏览器发送导航字段。 模型:
public class Friend
{
public int Id { get; set; }
.....
public virtual IEnumerable<VideoCollection> videoCollections { get; set; }
}
public class VideoCollection
{
public int Id { get; set; }
......
public int FriendId { get; set; }
public virtual Friend Friend { get; set; }
}
我的上下文类:
public class WebApiMySQLContext : DbContext
{
public WebApiMySQLContext(DbContextOptions<WebApiMySQLContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Friend>()
.HasMany(f => f.videoCollections)
.WithOne(v => v.Friend)
.HasForeignKey(v => v.FriendId)
.IsRequired();
}
public DbSet<Friend> Friends { get; set; } = null!;
public DbSet<VideoCollection> Videos { get; set; } = null!;
}
和用于后操作的控制器:
[HttpPost]
public async Task<ActionResult<VideoCollection>> PostVideoCollection(VideoCollection videoCollection)
{
if (_context.Videos == null)
{
return Problem("Entity set 'WebApiMySQLContext.Videos' is null.");
}
_context.Videos.Add(videoCollection);
await _context.SaveChangesAsync();
return CreatedAtAction("GetVideoCollection", new { id = videoCollection.Id }, videoCollection);
}
每个有效的邮政
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-6d1fcffc19ecf7f8e9bbf5eed016d842-f3712183c476ca8d-00",
"errors": {
"Friend": [
"The Friend field is required."
]
}
}
请求主体的响应主体
{
"movietitle": "movie",
"yearreleased": "1810",
"rating": "1.6",
"subject": "Violence",
"length": "1.8",
"note": "Bad film",
"friendid": "1"
}
So, I'm exploring ASP.NET Core (WebAPI). I created 2 models, made context file, added a one-to-many relationship between classes Friend and VideoCollection. Now, when I try to post create data into both Tables, I get validation errors since I don't send the navigation fields from the browser.
Models:
public class Friend
{
public int Id { get; set; }
.....
public virtual IEnumerable<VideoCollection> videoCollections { get; set; }
}
public class VideoCollection
{
public int Id { get; set; }
......
public int FriendId { get; set; }
public virtual Friend Friend { get; set; }
}
My Context Class:
public class WebApiMySQLContext : DbContext
{
public WebApiMySQLContext(DbContextOptions<WebApiMySQLContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Friend>()
.HasMany(f => f.videoCollections)
.WithOne(v => v.Friend)
.HasForeignKey(v => v.FriendId)
.IsRequired();
}
public DbSet<Friend> Friends { get; set; } = null!;
public DbSet<VideoCollection> Videos { get; set; } = null!;
}
And Controller for POST operation:
[HttpPost]
public async Task<ActionResult<VideoCollection>> PostVideoCollection(VideoCollection videoCollection)
{
if (_context.Videos == null)
{
return Problem("Entity set 'WebApiMySQLContext.Videos' is null.");
}
_context.Videos.Add(videoCollection);
await _context.SaveChangesAsync();
return CreatedAtAction("GetVideoCollection", new { id = videoCollection.Id }, videoCollection);
}
Response Body for every valid POST
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-6d1fcffc19ecf7f8e9bbf5eed016d842-f3712183c476ca8d-00",
"errors": {
"Friend": [
"The Friend field is required."
]
}
}
Request Body for POST videos
{
"movietitle": "movie",
"yearreleased": "1810",
"rating": "1.6",
"subject": "Violence",
"length": "1.8",
"note": "Bad film",
"friendid": "1"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用ViewModels,然后可以使用AutoMapper或手动映射(自动应用程序)
之后,您应该映射它...
添加时不要使用任何导航属性。
You should use ViewModels and then you can map it by using AutoMapper or manually (How to use AutoMapper)
After this, you should map it...
Don't use any of the navigation properties while adding or something like that.