Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6648415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:39:09+00:00 2026-05-26T00:39:09+00:00

I have a problem with using EF 4.1 and many to many relationship. I

  • 0

I have a problem with using EF 4.1 and many to many relationship. I decided to use database first approach (although I got the same problem with code first approach), and I created a model of the database:

enter image description here

I have a many to many relationship between Picture and Tag tables. In the DB it is created as a new table called “PictureTag” with fields “Picture_Id” and “Tag_Id”.

I am adding data to my database with the following piece of code:

if (tags != null)
{
    foreach (HtmlNode tagNode in tags)
    {
        string tagString = tagNode.InnerText.Remove(0, 1);
        Tag tag;
        if (db.Tags.Any(q => q.TagName == tagString))
        {
            tag = db.Tags.Single(q => q.TagName == tagString);
        }
        else
        {
            tag = new Tag { TagName = tagString };
        }
        pic.Tags.Add(tag);
    }
}
List<DbEntityValidationResult> list = db.GetValidationErrors().ToList();
db.Pictures.Add(pic);
db.SaveChanges();

This is of course just a part of my code. It is a part of my Crawler function. The code works fine, doesn’t generate any errors, and works on a single DB context.

After this function is done I am able to see data in my DB, all three pictures, tags and entities in PictureTag table.

Yet when I try to put on my view pictures, and tags that correspond to that picture, than I can’t see any tags. Like none of the pictures had any tags attached to them. I know it is not true, because I see data in my DB, so I don’t know what is wrong…

This is the code fr my view:

@model IEnumerable<SikSolution.Picture>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@foreach (var item in Model)
{

    <div>
        <img src="@item.Link" alt="@item.Name" />
        <br />
        <span>
            Tags: @foreach (var tag in item.Tags)
                  {
                      Html.Label(tag.TagName + ", ");
                  }
        </span>
    </div> 
}

I really don’t know what is wrong… I would appreciate any advice or pointing me in the right direction.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:39:09+00:00Added an answer on May 26, 2026 at 12:39 am

    Possible explanation is that you don’t use lazy loading and also didn’t load your Picture collection including the related tags (eager loading). It should look like this:

    //...
    var pictures = db.Pictures.Include("Tags") // or Include(p => p.Tags) in EF 4.1
        .Where(...)
        .ToList();
    
    return View(pictures);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem using the SSIS. I try to import data from database
I have a problem using JSON and arrays. Here is my code: while($row =
i have a problem using the Catch Clipboard Events code found on this link
I have a problem with using the prefix, seems like whenever I use the
I have a model withs Parents, Children and Grandchildren, in a many-to-many relationship. Using
I am using CakePHP 2.0 and I have a many-to-many relationship betweens users and
Using the Entity Framework Code First paradigm I have defined the following objects and
I have come across a weird problem concerning composite keys and one-to-many relationship. I
My database contains 3 tables: User and Service entities have many-to-many relationship and are
I have the following problem using a has_many :through many-to-many relation in a multi-select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.