Asp.Net Core EF Core many to many relation update command

asp.net-core entity-framework-core

Question

I followed this link enter link description here to create many to many relationship. But, I do not know how to create and update Tag value to Post Object.
Any help would be appreciated.

Update, related code

class MyContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public DbSet<Tag> Tags { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PostTag>()
            .HasKey(t => new { t.PostId, t.TagId });

        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Post)
            .WithMany(p => p.PostTags)
            .HasForeignKey(pt => pt.PostId);

        modelBuilder.Entity<PostTag>()
            .HasOne(pt => pt.Tag)
            .WithMany(t => t.PostTags)
            .HasForeignKey(pt => pt.TagId);
    }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public List<PostTag> PostTags { get; set; }
}

public class Tag
{
    public string TagId { get; set; }

    public List<PostTag> PostTags { get; set; }
}

public class PostTag
{
    public int PostId { get; set; }
    public Post Post { get; set; }

    public string TagId { get; set; }
    public Tag Tag { get; set; }
}

Update2: Code for Update record With below code, it will create records in both three tables.

    var p = new Post { Content = "C1" };
    var t = new Tag { TagId = "T1" };
    var pt = new PostTag { Post = p, Tag = t };
    _context.PostTag.Add(pt);
    _context.SaveChanges();

But, with below code, it will insert new records in middle table PostTag instead of update the previous records.

    var t1 = new Tag { TagId = "T3" };
    var t2 = new Tag { TagId = "T4" };

    var p =_context.Posts.Find(1);
    p.PostTags = new List<PostTag>() {
        new PostTag{ Post=p, Tag=t1},
        new PostTag{ Post=p, Tag=t2}
    };
    _context.Posts.Update(p);
    _context.SaveChanges();
1
0
6/5/2017 7:32:34 AM

Accepted Answer

Let's create some sample data

var p = new Post { ... };
var t = new Tag { ... };
var pt = new PostTag { Post = p, Tag = t };

This is still in-memory so we have to add it to the context, starting with:

db.Posts.Add(p);
db.Tags.Add(t);

Note that at this stage neither p nor t has any knowledge of pt. There are two ways to insert the third entity into your database.

1) The easiest way is to add the DbSet<PostTag> property and then the line

db.PostTags.Add(pt);

2) Without that extra DbSet you will need to ensure the linking from the other 2 sides. You also need to set up the List properties:

p.PostTags = new List<PostTag> { pt };
t.PostTags = new List<PostTag> { pt };

EF will now fill out any missing link fields.

1
6/6/2017 5:22:55 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow