public async Task Create([Bind("Name,IsPublished,CretedDate,ModifiyDate,posts")] Tag tag,List posts)
{
if (ModelState.IsValid)
{
foreach (var item in posts)
{
var _post = _contextPosts.dbcontext.Post.Find(item); // fetch one record from posts
tag.PostTag = new List //Tag
{
new PostTag
{
Post = _post,
Tag = tag
}
};
_contextTags.dbcontext.Add(tag);
await _contextTags.dbcontext.SaveChangesAsync();
}
}
}

Таблицы
public class Post
{
[Key]
public int Id { get; set; }
[Display(Name = "Created By:")]
public AppUser AuthorId { get; set; }
[Required]
public string Title { get; set; }
public string metaTitle { get; set; }
[Required]
public string Body { get; set; }
public bool IsPublished { get; set; } = true;
public bool IsFeatured { get; set; } = false;
public DateTime CretedDate { get; set; } = DateTime.Now;
public DateTime ModifiyDate { get; set; } = DateTime.Now;
public ICollection Comments { get; set; }
public ICollection Tags { get; set; }
}
public class Tag
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public bool IsPublished { get; set; } = true;
public DateTime CretedDate { get; set; } = DateTime.Now;
public DateTime ModifiyDate { get; set; } = DateTime.Now;
public ICollection PostTag { get; set; }
public ICollection Images { get; set; }
}
public class PostTag
{
public int TagId { get; set; }
public int PostId { get; set; }
public Post Post { get; set; }
public Tag Tag { get; set; }
public AppUser AppUser { get; set; }
}
Подробнее здесь: https://stackoverflow.com/questions/639 ... ity-insert
Мобильная версия