需要朋友字段。 (导航属性字段)

发布于 2025-02-06 11:05:29 字数 2299 浏览 1 评论 0原文

因此,我正在探索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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2025-02-13 11:05:30

您应该使用ViewModels,然后可以使用AutoMapper或手动映射(自动应用程序

public class FriendViewModel
{
  public int Id {get;set;}
  .....
  (WITHOUT IEnumerable<VideoCollection> videoCollections property)
}
public class VideoCollectionViewModel
{
  public int Id { get;set;}
  ...
  public int FriendId {get;set;}

  (WITHOUT Friend property)
}

之后,您应该映射它...
添加时不要使用任何导航属性。

You should use ViewModels and then you can map it by using AutoMapper or manually (How to use AutoMapper)

public class FriendViewModel
{
  public int Id {get;set;}
  .....
  (WITHOUT IEnumerable<VideoCollection> videoCollections property)
}
public class VideoCollectionViewModel
{
  public int Id { get;set;}
  ...
  public int FriendId {get;set;}

  (WITHOUT Friend property)
}

After this, you should map it...
Don't use any of the navigation properties while adding or something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文