简体   繁体   English

使用 .net core Entity Framework 从 JSON 创建嵌套数据

[英]Create nested data from JSON using .net core Entity Framework

I wan't to create a "Group" using http POST request.我不想使用 http POST 请求创建“组”。 The User isn't existing, so it should also be created.用户不存在,因此也应该创建它。 But the "item" in received by Post is alway null.但是 Post 收到的“项目”始终为空。 When I don't post a user or an empty users array, it works.当我不发布用户或空的用户数组时,它会起作用。

Here is my Model:这是我的模型:

public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual Group Group { get; set; }
}

public class Group
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual List<User> Users { get; set; }
}

And the Post Method:和 Post 方法:

// POST api/<controller>
[HttpPost]
[ProducesResponseType(typeof(Group), StatusCodes.Status200OK)]
public async Task<IActionResult> Post([FromBody] Group item)
{
    EntityEntry<Group> entityEntry = this._launcherContext.Database.Add(item);
    this._launcherContext.Database.SaveChanges();

    return Ok(entityEntry.Entity);
}

And at least the json body:至少是 json 正文:

{
  "name": "TestGroup",
  "users": [
    {
      "name": "Threepwood, Guybrush"
    }
  ]
}

If you want to create Group without User create a GroupPost class.如果要创建没有用户的组,请创建一个 GroupPost 类。

public class GroupPost
{
    public GroupPost(){
    Users = new List<UserPost>();
    }
    public string Name { get; set; }
    public List<UserPost> Users { get; set; }
}
public class UserPost
{
    public string Name { get; set; }

}    

[HttpPost]
[ProducesResponseType(typeof(Group), StatusCodes.Status200OK)]
public async Task<IActionResult> Post([FromBody] GroupPost item)
{
    Group newItem = new Group
    {
     Name=item.Name,         
    };
    foreach(var user in item.Users){
    User newUser = new User{
    Name=user.Name
    };
    newItem.Add(newUser);
    }
    EntityEntry<Group> entityEntry = this._launcherContext.Database.Add(newItem);
    this._launcherContext.Database.SaveChanges();

    return Ok(entityEntry.Entity);
}

Then you can post your data without User list然后您可以在没有用户列表的情况下发布您的数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从嵌套类创建动态实体以绑定 Devexpress.net 核心数据网格 - Create dynamic entity from nested classes to bind Devexpress net core Data Grid .NET Core Entity Framework 如何从模型创建表 - How does .NET Core Entity Framework Create Tables From Model 使用Entity Framework和ASP.NET Core从具有特定条件的其他表中查询数据 - Query data from other table with certain condition using Entity Framework and ASP.NET Core 如何使用 Entity Framework Core 在 .NET Core WebAPI 中返回嵌套表(一对多)? - How to return nested tables (one-to-many) in .NET Core WebAPI using Entity Framework Core? 如何使用 Entity Framework Core 从 sql 脚本创建数据库? - How to create a database from sql script using Entity Framework Core? 如何使用实体框架在 .NET Core 6 中播种数据? - How to seed data in .NET Core 6 with Entity Framework? .NET 核心实体框架播种数据关系 - .NET Core Entity Framework seeding data relations ASP.NET 核心 Web API - 使用实体框架在 .NET 核心 6 中播种数据 - ASP.NET Core Web API - seeding data in .NET Core 6 using Entity Framework C# - 使用 Entity Framework Core 3 HasConversion 将字段转换为 JSON in.Net Core 3.1 - C# - Using Entity Framework Core 3 HasConversion to convert a field to JSON in .Net Core 3.1 如何使用 Entity Framework Core 在 ASP.NET Core 中创建动态 select 框? - How to create dynamic select box in ASP.NET Core using Entity Framework Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM