locked
Did many-to-many change between EF 5.0.2 and EF 5.0.3? RRS feed

  • Question

  • User306743125 posted

    Hi

    Using EF Core 5.0.3

    From EF 5, many-to-many relationships are supported.
    The following worked under EF 5.0.2 but returns "Object not reference not set to an instance of the Object" with EF 5.0.3

    var p = new Post()
                {
                    Name = "new Post"
                };
    
                var t = new Tag()
                {
                    Name = "some Tag"
                };
                p.Tags.Add(t);

    I noticed that in the Join table; EF 5.0.3 have the Foreign Keys as FK_PostTag_Posts_PostsId and FK_PostTag_Tags_TagsId

    But EF 5.0.2 had it singuar as FK_PostTag_Posts_PostId and FK_PostTag_Tags_TagId 
    Anyone know whether something changed, or how to now add an item to the many-to-many relationship?

    Sunday, February 21, 2021 12:09 PM

Answers

  • User306743125 posted

    Ok, it works if you specifically use the Include function.

    something like 

    var post = _db.Posts.Include(t => t.Tags).FirstOrDefault(p =>p.Id == postToEdit.Id);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 21, 2021 6:09 PM