locked
How to code asp.net Core 5 new Many to many relationship RRS feed

  • Question

  • User306743125 posted

    Hi, asp.net core 5 now allows for many to many relationships.

    I understand the model configuration but need help on how to actually make us of the relationship in the controller and views.

    public class Post
        {
            public int ID { get; set; }
            public string PostName { get; set; }
    
            public ICollection<Tag> Tags { get; set; }
    }
    public class Tag
    { 
    public int ID { get; set; } 
    public string TagName { get; set; }
    
    public ICollection<Post> Posts { get; set; } 
    }

    And I can see that EF created a PostTag table in the database.
    I ran the scaffolding and created some Posts and created some Tags.

    But now I am stuck at added a Tag to a Post, where this is straight forward (and the setup done by the scaffolding) for one to many relationships.

    Let's say I want to Edit a Post and from there add some Tags to it, how would one do that?
    With a one to many relationship, you would have added TagID as a property to the Post model.

     [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Edit(int id, [Bind("ID,PostName")] Post post)
            {
                if (id != post.ID)
                {
                    return NotFound();
                }
    
                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(post);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!PostExists(post.ID))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return RedirectToAction(nameof(Index));
                }
                return View(post);
            }

    I have no idea what to add to the View to select the Tags nor what I should add to the Controller to Edit the post and link some Tags.

    Sunday, January 31, 2021 12:28 PM

Answers

All replies

  • User475983607 posted

    Let's say I want to Edit a Post and from there add some Tags to it, how would one do that?
    With a one to many relationship, you would have added TagID as a property to the Post model.

    I strongly recommend going through the Getting Started tutorials found on this site.  The tutorials covers reading and updating related records.  

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0

    There are several MVC constructs that you need learn and the tutorial does a very good job of explaining the programming patterns.

    Sunday, January 31, 2021 12:46 PM
  • User306743125 posted

    Thanks for the reply. 

    I guess my questions are:

    1) Does asp.net Core 5 now support many to many relationships?

    2) Does that mean you no longer have to create a 3rd entity, like below, to support the relationship?

    public class TagAssignment
    { 
    public int ID { get; set; } 
    public int PostID { get; set; } 
    public string TagID { get; set; }
    
    public Post post { get; set; } 
    public Tag tag { get; set; } 
    }

    3) It appears EF now creates PostTag table to manage the relationship.

    So what I am confused by (not having done programming for a couple of years), is how you read and write to this PostTag table.
    I am looking at the tutorial you referred to, but that still appears to have created a 3rd Entity "CourseAssignment" to manage the many to many relationship between "Instructors" and "Courses"

    Sunday, January 31, 2021 2:07 PM
  • User475983607 posted

    2) Does that mean you no longer have to create a 3rd entity, like below, to support the relationship?

    No. Many-to-many relationships have always (and still do) require three tables.   

    So what I am confused by (not having done programming for a couple of years), is how you read and write to this PostTag table.

    Assuming code first, it depends on how you designed the navigation properties.  

    See the following blog.

    https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration

    EF Core Relationships reference.

    https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 31, 2021 2:52 PM
  • User306743125 posted

    Thanks,

    There is a good intro to changes that Core 5 brought to many to many relationships in the video below (at about the halfway mark)

    https://channel9.msdn.com/Events/dotnetConf/2020/Entity-Framework-Core-50-The-Next-Generation-for-Data-Access

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 31, 2021 4:12 PM