What you are missing a conversion layer. Let me explain.
Your post object stored in the database has to have the following structure:
Post Table
+------------+----------------+-------+----------+
| PostId | ExternalPostId | Title | Content |
+------------+----------------+-------+----------+
- PostId - This is the primary key within your own table. Auto-generated, required.
- ExternalPostId - this is the PostId as it arrives from the API.
- Title - this is the title as it arrives from the API
- Content - this is the content as it arrives from the API.
Tag table
+--------+---------------+-------------+-------------+
| TagId | ExternalTagId | OtherField1 | OtherField2 |
+--------+---------------+-------------+-------------+
- TagId - This is the primary key of your own Tag table. Autogenerated, required.
- ExternalTagId - this is the TagId as it arrives from the API
- OtherField1, OtherField2 - whatever other fields arrive with the tag from the API.
Join Table
+------------+-------+
| PostId | TagId |
+------------+-------+
- PostId, TagId - Composite Primary key
- PostId - foreign key, required, non-auto-numbered referencing the
Post
table.
- TagId - foreign key, required, non-auto-numbered referencing the
Tag
table.
At that point your entities will look like this:
Post Enity
[Table("Post")]
public class DbPost {
[Key]
[Column("PostId")]
public int Id { get; set; }
[Column("ExternalPostId")]
public int ExternalId { get; set; }
[Column("Title")]
public string Title { get; set; }
[Column("Content")]
public string Content { get; set; }
public IList<PostTag> PostTags { get; set; }
}
Tag entity:
[Table("Tag")
public class DbTag
{
[Key]
[Column("TagId")]
public int Id { get; set; }
[Column("ExternalTagId")]
public string ExternalId { get; set; }
public IList<PostTag> PostTags { get; set; }
}
PostTag Entity
This entity stays the same. The difference here is that PostId is mapped to the Id property of the Post entity, which is your own primary key of your own Post table. In a similar fashion the TagId is mapped to the Id property of the Tag table, which is also your own primary key, of your own table.
public class DbPostTag
{
public int PostId { get; set; }
public Post Post { get; set; }
public int TagId { get; set; }
public Tag Tag { get; set; }
}
Finally, your domain context:
public class Database : DbContext {
public virtual DbSet<DbPost> Posts { get; set; }
public virtual DbSet<DbTag> Tags { get; set; }
public virtual DbSet<DbPostTag> PostTags { get; set; }
...
}
When you insert a post from the API you need to create a translation between the post from the API to the post entity in your database. You can do so via an extension method or a method that translates this for you:
public static class ApiObjectExtensions {
public static DbPost ToDbPost(this Post post){
var dbPost = new DbPost(){
ExternalId = post.PostId,
Title = post.Title,
Content = post.Content,
PostTags = new List<DbPostTags>()
};
}
public static DbTag ToDbTag(this Tag tag){
return new DbTag(){
ExternalId = tag.Id,
PostTags = new List<DbPostTags>()
};
}
}
So when you are inserting the data in the domain context you simply have to convert the corresponding entities. This code assumes that you really have the object graph described above and your post has a bunch of post tags, and the each post tag has a bunch of tags.
foreach(var post in posts){
var dbPost = post.ToDbPost();
if(post.PostTags != null && post.PostTags.Any()){
foreach(var postTag in post.PostTags){
if(postTag.Tag != null){
var dbTag = postTag.Tag.ToDbTag();
var dbPostTag = new DbPostTag(){
Post = dbPost,
Tag = dbTag
};
dbPost.PostTags.Add(dbPostTag);
}
}
}
Database.DbPosts.Add(dbPost);
}
Database.SaveChanges();
If you are receiving the posts, the post tags, and the tags in separate calls than your insert operation will be a bit different:
var insertedPosts = new List<DbPost>();
var insertedTags = new List<DbTag>();
foreach(var post in posts){
var dbPost = post.ToDbPost();
Database.Posts.Add(dbPost);
insertedPosts.Add(dbPost);
}
foreach(var tag in tags){
var dbTag = tag.ToDbTag();
Database.Tags.Add(dbTag);
insertedTags.Add(dbTag);
}
Database.SaveChanges();
This will write all db post and db tag entities to the db and assign their primary keys.
The next step would be to link the two together.
foreach(var postTag in postTags){
var dbPost = insertedPosts.FirstOrDefault(p => p.ExternalId = postTag.PostId);
var dbTag = insertedTags.FirstOrDefault(t => t.ExternalId = postTag.TagId);
if(dbPost != null && dbTag != null){
var dbPostTag = new DbPostTag(){
PostId = dbPost.Id,
Tag = dbTag.Id
};
Database.PostTags.Add(dbPostTag);
}
}
Database.SaveChanges();
The last piece is when you retrieve the data back and you want it in the original format.
The following query will return all posts, their linking entities, and their tags:
var dbPosts = this.Database.Posts
.Include( p => p.PostTags )
.ThenInclude(pt => pt.Tag )
.ToList();
var apiPosts = dbPosts.Select(p => p.ToApiPost()).ToList();
To convert the stuff back to the original Post, PostTag, and Tag types you can simply create extension methods as follows:
public static class DbObjectExtensions {
public static Post ToApiPost(this DbPost dbPost){
var post = new Post(){
PostId = dbPost.ExternalId,
Title = dbPost.Title,
Content = dbPost.Content,
PostTags = new List<PostTag>()
};
if(dbPost.PostTags != null) {
foreach(var dbPostTag in dbPost.PostTags){
if(dbPostTag.Tag != null){
var tag = dbPostTag.Tag.ToApiTag();
var postTag = new PostTag(){
PostId = post.PostId,
Post = post,
TagId = tag.TagId,
Tag = tag
};
post.PostTags.Add(postTag);
tag.PostTags.Add(postTag);
}
}
}
return post;
}
public static Tag ToApiTag(this DbTag dbTag){
return new Tag(){
TagId = dbTag.ExternalId
};
}
}
Post
should containList<Tag>
notList<PostTag>