I'm very new with EF Core 5 but the first thing I needed to do was TO post a new item/object which has child items (Many-to-many).
I have set up my models like this:
public class Post
{
public int id { get; set; }
public string name { get; set; }
public ICollection<Tag> tags { get; set; }
}
public class Tag
{
public int id { get; set; }
public string name { get; set; }
[JsonIgnore]
public ICollection<Post> posts { get; set; }
}
...and the DbContext is very simple:
public DbSet<Post> posts { get; set; }
public DbSet<Tag> tags { get; set; }
And the migrations work as expected, creating the join table automatically. But I can't figure out for the life of me how to post a new item:
This is the json ob posted by the client:
{
"name": "New Post Name"
"tags": [
{
"id": 11
},
{
"id": 12
},
{
"id": 16
},
{
"id": 19
}
]
}
The User will have entered a Name for the Post in a HTML form and selected a number of Tags available in a check box list.
....and in my controller:
[HttpPost]
public async Task<ActionResult<Post>> PostPost(Post post)
{
// can anybody help and tell me what goes in here? Cheers
}
Thanks a lot in advance
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…